1+ /*
2+ *
3+ * Copyright (c) Innovar Healthcare. All rights reserved.
4+ *
5+ * https://www.innovarhealthcare.com
6+ *
7+ * The software in this package is published under the terms of the MPL license a copy of which has
8+ * been included with this distribution in the LICENSE.txt file.
9+ */
10+
11+ package com .mirth .connect .plugins .dynamiclookup .client .dialog ;
12+
13+ import java .awt .Dimension ;
14+ import java .awt .event .ItemEvent ;
15+ import java .awt .event .ItemListener ;
16+ import java .io .File ;
17+ import java .util .Properties ;
18+
19+ import javax .swing .ButtonGroup ;
20+ import javax .swing .JButton ;
21+ import javax .swing .JComboBox ;
22+ import javax .swing .JFileChooser ;
23+ import javax .swing .JLabel ;
24+ import javax .swing .JPanel ;
25+ import javax .swing .JRadioButton ;
26+ import javax .swing .JTextField ;
27+ import javax .swing .WindowConstants ;
28+ import javax .swing .filechooser .FileNameExtensionFilter ;
29+
30+ import com .mirth .connect .client .ui .Frame ;
31+ import com .mirth .connect .client .ui .MirthDialog ;
32+ import com .mirth .connect .client .ui .UIConstants ;
33+
34+ import net .miginfocom .swing .MigLayout ;
35+
36+ /**
37+ * @author Jim(Zi Min) Weng
38+ * @create 2025-08-22 11:12 AM
39+ */
40+ public class ImportLookupGroupDialog extends MirthDialog {
41+ private Frame parent ;
42+ private String [] defaultGroupNames = { "Race" , "Ethnicity" , "Administrative Gender" , "Marital Status" , "Religion" };
43+ private boolean saved = false ;
44+ private JLabel importFromLabel ;
45+ private JLabel fromSystemLabel ;
46+ private JLabel fromFileLabel ;
47+ private JRadioButton fromSystemRadioButton ;
48+ private JRadioButton fromFileRadioButton ;
49+ private JButton browseFileButton ;
50+ private JTextField filePathField ;
51+ private JComboBox <String > defaultGroupComboBox ;
52+ private JButton importButton ;
53+ private JButton cancelButton ;
54+ private Properties importProperties ;
55+
56+ public ImportLookupGroupDialog (Frame parent ) {
57+ super (parent , true );
58+
59+ this .parent = parent ;
60+
61+ initComponents ();
62+ initLayout ();
63+
64+ setDefaultCloseOperation (WindowConstants .DISPOSE_ON_CLOSE );
65+ setTitle (String .format ("Import Default Lookup Group" ));
66+ pack ();
67+ setLocationRelativeTo (parent );
68+ setVisible (true );
69+ }
70+
71+ public void initComponents () {
72+ setBackground (UIConstants .BACKGROUND_COLOR );
73+ getContentPane ().setBackground (getBackground ());
74+
75+ // --- Radio buttons for source selection ---
76+ importFromLabel = new JLabel ("Import Lookup Group from:" );
77+ fromSystemRadioButton = new JRadioButton ("System" );
78+ fromSystemRadioButton .setBackground (getBackground ());
79+
80+ fromFileRadioButton = new JRadioButton ("File" );
81+ fromFileRadioButton .setBackground (getBackground ());
82+
83+ ButtonGroup sourceGroup = new ButtonGroup ();
84+ sourceGroup .add (fromSystemRadioButton );
85+ sourceGroup .add (fromFileRadioButton );
86+
87+ // Default: System selected
88+ fromSystemRadioButton .setSelected (true );
89+
90+ // --- File section ---
91+ fromFileLabel = new JLabel ("Import from file:" );
92+ filePathField = new JTextField ();
93+ browseFileButton = new JButton ("Browse" );
94+ browseFileButton .addActionListener (e -> {
95+ JFileChooser importFileChooser = new JFileChooser ();
96+ importFileChooser .setFileFilter (new FileNameExtensionFilter ("JSON files" , "json" ));
97+
98+ // Restore last-used directory if available
99+ File currentDir = new File (parent .userPreferences .get ("currentDirectory" , "" ));
100+ if (currentDir .exists ()) {
101+ importFileChooser .setCurrentDirectory (currentDir );
102+ }
103+
104+ if (importFileChooser .showOpenDialog (parent ) == JFileChooser .APPROVE_OPTION ) {
105+ File selectedFile = importFileChooser .getSelectedFile ();
106+
107+ // Save directory for next time
108+ Frame .userPreferences .put ("currentDirectory" , selectedFile .getParent ());
109+
110+ // Set path in text field
111+ filePathField .setText (selectedFile .getAbsolutePath ());
112+ }
113+
114+ });
115+
116+ // --- System section ---
117+ fromSystemLabel = new JLabel ("Import from system:" );
118+ defaultGroupComboBox = new JComboBox <>(defaultGroupNames );
119+
120+ // --- Buttons ---
121+ importButton = new JButton ("Import" );
122+ cancelButton = new JButton ("Cancel" );
123+
124+ importButton .addActionListener (e -> {
125+ // Implement import logic here
126+ setImportProperties (new Properties ());
127+ saved = true ;
128+ dispose ();
129+ });
130+
131+ cancelButton .addActionListener (e -> dispose ());
132+
133+ ItemListener stateListener = e -> {
134+ if (e .getStateChange () == ItemEvent .SELECTED )
135+ handleSourceToggle ();
136+ };
137+
138+ fromSystemRadioButton .addItemListener (stateListener );
139+ fromFileRadioButton .addItemListener (stateListener );
140+
141+ // Ensure initial UI matches the default selection
142+ handleSourceToggle ();
143+ }
144+
145+ private void handleSourceToggle () {
146+
147+ boolean systemSelected = fromSystemRadioButton .isSelected ();
148+
149+ fromSystemLabel .setEnabled (systemSelected );
150+ defaultGroupComboBox .setEnabled (systemSelected );
151+
152+ fromFileLabel .setEnabled (!systemSelected );
153+ filePathField .setEditable (!systemSelected );
154+ browseFileButton .setEnabled (!systemSelected );
155+
156+ // A simple revalidate/repaint of the dialog is enough
157+ revalidate ();
158+ repaint ();
159+ }
160+
161+ public void initLayout () {
162+ // Main panel: 2 columns (labels left, inputs aligned)
163+ JPanel mainPanel = new JPanel (new MigLayout ("insets 20, fill, wrap 2" , "[left]10[grow, fill]" // col1 = left-aligned labels, col2 = aligned input fields
164+ ));
165+ mainPanel .setBackground (getBackground ());
166+
167+ // --- Radio buttons section ---
168+ JPanel radioPanel = new JPanel (new MigLayout ("insets 0, wrap 3" , "[][][]" ));
169+ radioPanel .setBackground (getBackground ());
170+ radioPanel .add (importFromLabel );
171+ radioPanel .add (fromSystemRadioButton );
172+ radioPanel .add (fromFileRadioButton );
173+
174+ // full-width row for radios
175+ mainPanel .add (radioPanel , "span, align left, wrap" );
176+
177+ // --- File Import Section ---
178+ JLabel fromFileLabel = new JLabel ("Import from file:" );
179+ mainPanel .add (fromFileLabel );
180+ mainPanel .add (filePathField , "growx, split 2" ); // input starts aligned
181+ mainPanel .add (browseFileButton , "wrap" ); // browse button next to field
182+
183+ // --- System Selection Section ---
184+ JLabel fromSystemLabel = new JLabel ("Import from system:" );
185+ mainPanel .add (fromSystemLabel );
186+ mainPanel .add (defaultGroupComboBox , "growx, wrap" ); // dropdown starts aligned
187+
188+ // --- Action Buttons ---
189+ JPanel buttonPanel = new JPanel (new MigLayout ("insets 0" , "[right]" ));
190+ buttonPanel .setBackground (getBackground ());
191+ buttonPanel .add (importButton , "tag ok" );
192+ buttonPanel .add (cancelButton , "tag cancel, gapleft 10" );
193+
194+ mainPanel .add (buttonPanel , "span, align right" );
195+
196+ // --- Add to dialog ---
197+ getContentPane ().add (mainPanel );
198+
199+ // --- Enlarge the dialog ---
200+ setPreferredSize (new Dimension (500 , 220 ));
201+ pack ();
202+ setLocationRelativeTo (null ); // Center on screen (optional)
203+ }
204+
205+ public boolean isSaved () {
206+ return saved ;
207+ }
208+
209+ private void setImportProperties (Properties properties ) {
210+ this .importProperties = new Properties ();
211+ this .importProperties .setProperty ("importMethod" , fromSystemRadioButton .isSelected () ? "system" : "file" );
212+ this .importProperties .setProperty ("filePath" , filePathField .getText ());
213+ this .importProperties .setProperty ("defaultGroup" , (String ) defaultGroupComboBox .getSelectedItem ());
214+ }
215+
216+ public Properties getImportProperties () {
217+ return importProperties ;
218+ }
219+ }
0 commit comments