KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > dialogs > ProjectPropertiesDialog


1 package com.genimen.djeneric.tools.generator.dialogs;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Frame JavaDoc;
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.io.File JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.swing.DefaultComboBoxModel JavaDoc;
15 import javax.swing.DefaultListModel JavaDoc;
16 import javax.swing.JButton JavaDoc;
17 import javax.swing.JCheckBox JavaDoc;
18 import javax.swing.JComboBox JavaDoc;
19 import javax.swing.JDialog JavaDoc;
20 import javax.swing.JFileChooser JavaDoc;
21 import javax.swing.JLabel JavaDoc;
22 import javax.swing.JList JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JScrollPane JavaDoc;
25 import javax.swing.JTabbedPane JavaDoc;
26 import javax.swing.JTable JavaDoc;
27 import javax.swing.JTextField JavaDoc;
28
29 import com.genimen.djeneric.tools.generator.util.ProjectProperties;
30 import com.genimen.djeneric.tools.generator.util.TemplateSet;
31 import com.genimen.djeneric.tools.generator.util.TemplateSetManager;
32 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
33 import com.genimen.djeneric.util.DjLogger;
34
35 public class ProjectPropertiesDialog extends JDialog JavaDoc
36 {
37   private static final long serialVersionUID = 1L;
38   JPanel JavaDoc panel1 = new JPanel JavaDoc();
39   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
40   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
41   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
42   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
43   JButton JavaDoc _butOk = new JButton JavaDoc();
44   JButton JavaDoc _butCancel = new JButton JavaDoc();
45   JTabbedPane JavaDoc _tabs = new JTabbedPane JavaDoc();
46   JPanel JavaDoc _pnlGeneral = new JPanel JavaDoc();
47   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
48   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
49   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
50   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
51   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
52   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
53   JTextField JavaDoc _edtOutputPath = new JTextField JavaDoc();
54   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
55   JPanel JavaDoc jPanel6 = new JPanel JavaDoc();
56   JButton JavaDoc _butOutputPath = new JButton JavaDoc();
57   JPanel JavaDoc _pnlTemplates = new JPanel JavaDoc();
58   BorderLayout JavaDoc borderLayout5 = new BorderLayout JavaDoc();
59   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
60   JList JavaDoc _lstTemplates = new JList JavaDoc();
61   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
62   DjVerticalFlowLayout verticalFlowLayout3 = new DjVerticalFlowLayout();
63   JButton JavaDoc _butAddTemplate = new JButton JavaDoc();
64   JButton JavaDoc _butRemoveTemplate = new JButton JavaDoc();
65   JPanel JavaDoc _pnlDefines = new JPanel JavaDoc();
66   JScrollPane JavaDoc jScrollPane2 = new JScrollPane JavaDoc();
67   BorderLayout JavaDoc borderLayout6 = new BorderLayout JavaDoc();
68   JTable JavaDoc jTable1 = new JTable JavaDoc();
69   JPanel JavaDoc jPanel7 = new JPanel JavaDoc();
70   DjVerticalFlowLayout verticalFlowLayout4 = new DjVerticalFlowLayout();
71   JButton JavaDoc _butAddDefine = new JButton JavaDoc();
72   JButton JavaDoc _butRemoveDefine = new JButton JavaDoc();
73   boolean _canceled = true;
74   ProjectProperties _projectProps;
75   DefaultListModel JavaDoc _templateListModel = new DefaultListModel JavaDoc();
76   TemplateSetManager _templateSetManager;
77   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
78   JComboBox JavaDoc _cbbOverwrite = new JComboBox JavaDoc();
79
80   String JavaDoc[] _overwriteOptions = new String JavaDoc[]{"Ask", "Always", "Never"};
81   DefaultComboBoxModel JavaDoc _cbbModel = new DefaultComboBoxModel JavaDoc(_overwriteOptions);
82   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
83   JCheckBox JavaDoc _chkOpenAfterGenerate = new JCheckBox JavaDoc();
84
85   public ProjectPropertiesDialog(Frame JavaDoc frame, String JavaDoc title, ProjectProperties projectProps,
86                                  TemplateSetManager templateSetManager)
87   {
88     super(frame, title, true);
89     try
90     {
91       _templateSetManager = templateSetManager;
92       _projectProps = projectProps;
93
94       jbInit();
95
96       setSize(375, 200);
97
98       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
99       Dimension JavaDoc frameSize = getSize();
100       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
101
102       _edtOutputPath.setText(_projectProps.getOutputPath());
103       _cbbOverwrite.setSelectedIndex(_projectProps.getOverwriteAction());
104       _chkOpenAfterGenerate.setSelected(_projectProps.isOpenAfterGenerate());
105       Iterator JavaDoc ti = _projectProps.getTemplateSetIterator();
106       while (ti.hasNext())
107         _templateListModel.addElement(ti.next());
108
109       // Iterator di = _projectProps.getDefineKeyIterator();
110
// while(di.hasNext()) _templateListModel.addElement(ti.next());
111

112       if (_templateListModel.getSize() > 0) _lstTemplates.setSelectedIndex(0);
113       setVisible(true);
114     }
115     catch (Exception JavaDoc ex)
116     {
117       DjLogger.log(ex);
118     }
119   }
120
121   void jbInit() throws Exception JavaDoc
122   {
123     panel1.setLayout(borderLayout1);
124     jPanel2.setLayout(borderLayout2);
125     _butOk.setPreferredSize(new Dimension JavaDoc(73, 27));
126     _butOk.setText("OK");
127     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
128     {
129       public void actionPerformed(ActionEvent JavaDoc e)
130       {
131         _butOk_actionPerformed(e);
132       }
133     });
134     _butCancel.setText("Cancel");
135     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
136     {
137       public void actionPerformed(ActionEvent JavaDoc e)
138       {
139         _butCancel_actionPerformed(e);
140       }
141     });
142     _pnlGeneral.setLayout(borderLayout3);
143     jPanel5.setLayout(verticalFlowLayout2);
144     jPanel4.setLayout(verticalFlowLayout1);
145     jLabel1.setPreferredSize(new Dimension JavaDoc(66, 21));
146     jLabel1.setText("Output path");
147     jPanel6.setLayout(borderLayout4);
148     _butOutputPath.setText("...");
149     _butOutputPath.addActionListener(new java.awt.event.ActionListener JavaDoc()
150     {
151       public void actionPerformed(ActionEvent JavaDoc e)
152       {
153         _butOutputPath_actionPerformed(e);
154       }
155     });
156     _butOutputPath.setPreferredSize(new Dimension JavaDoc(21, 21));
157     _pnlTemplates.setLayout(borderLayout5);
158     jPanel1.setLayout(verticalFlowLayout3);
159     _butAddTemplate.setText("Add");
160     _butAddTemplate.addActionListener(new java.awt.event.ActionListener JavaDoc()
161     {
162       public void actionPerformed(ActionEvent JavaDoc e)
163       {
164         _butAddTemplate_actionPerformed(e);
165       }
166     });
167     _butRemoveTemplate.setText("Remove");
168     _butRemoveTemplate.addActionListener(new java.awt.event.ActionListener JavaDoc()
169     {
170       public void actionPerformed(ActionEvent JavaDoc e)
171       {
172         _butRemoveTemplate_actionPerformed(e);
173       }
174     });
175     _pnlDefines.setLayout(borderLayout6);
176     jPanel7.setLayout(verticalFlowLayout4);
177     _butAddDefine.setText("Add");
178     _butRemoveDefine.setText("Remove");
179     _lstTemplates.setModel(_templateListModel);
180     jLabel2.setPreferredSize(new Dimension JavaDoc(56, 21));
181     jLabel2.setText("Overwrite");
182     _cbbOverwrite.setPreferredSize(new Dimension JavaDoc(130, 21));
183     _cbbOverwrite.setModel(_cbbModel);
184     jLabel3.setPreferredSize(new Dimension JavaDoc(114, 21));
185     jLabel3.setText("Open after generate");
186     _chkOpenAfterGenerate.setToolTipText("");
187     getContentPane().add(panel1);
188     panel1.add(jPanel2, BorderLayout.SOUTH);
189     jPanel2.add(jPanel3, BorderLayout.EAST);
190     jPanel3.add(_butCancel, null);
191     jPanel3.add(_butOk, null);
192     panel1.add(_tabs, BorderLayout.CENTER);
193     _tabs.add(_pnlGeneral, "General");
194     _pnlGeneral.add(jPanel4, BorderLayout.WEST);
195     jPanel4.add(jLabel1, null);
196     jPanel4.add(jLabel2, null);
197     jPanel4.add(jLabel3, null);
198     _pnlGeneral.add(jPanel5, BorderLayout.CENTER);
199     jPanel5.add(jPanel6, null);
200     jPanel6.add(_edtOutputPath, BorderLayout.CENTER);
201     jPanel6.add(_butOutputPath, BorderLayout.EAST);
202     jPanel5.add(_cbbOverwrite, null);
203     jPanel5.add(_chkOpenAfterGenerate, null);
204     _tabs.add(_pnlTemplates, "Template sets");
205     _pnlTemplates.add(jScrollPane1, BorderLayout.CENTER);
206     _pnlTemplates.add(jPanel1, BorderLayout.EAST);
207     jPanel1.add(_butAddTemplate, null);
208     jScrollPane1.getViewport().add(_lstTemplates, null);
209     jPanel1.add(_butRemoveTemplate, null);
210     _tabs.add(_pnlDefines, "Defines");
211     _pnlDefines.add(jScrollPane2, BorderLayout.CENTER);
212     _pnlDefines.add(jPanel7, BorderLayout.EAST);
213     jPanel7.add(_butAddDefine, null);
214     jPanel7.add(_butRemoveDefine, null);
215     jScrollPane2.getViewport().add(jTable1, null);
216   }
217
218   public boolean wasCancelled()
219   {
220     return _canceled;
221   }
222
223   void _butOutputPath_actionPerformed(ActionEvent JavaDoc e)
224   {
225     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
226     fc.setDialogTitle("Generate to path");
227     String JavaDoc path = _edtOutputPath.getText();
228     if (path.trim().length() == 0) path = System.getProperties().getProperty("user.dir");
229     fc.setCurrentDirectory(new File JavaDoc(path));
230     fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
231
232     if (fc.showDialog(this, "Generate to") != JFileChooser.CANCEL_OPTION)
233     {
234       _edtOutputPath.setText(fc.getSelectedFile().getAbsolutePath());
235     }
236   }
237
238   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
239   {
240     setVisible(false);
241   }
242
243   void _butOk_actionPerformed(ActionEvent JavaDoc e)
244   {
245     _canceled = false;
246     _projectProps.clear();
247     _projectProps.setOutputPath(_edtOutputPath.getText());
248     _projectProps.setOverwriteAction(_cbbOverwrite.getSelectedIndex());
249     _projectProps.setOpenAfterGenerate(_chkOpenAfterGenerate.isSelected());
250
251     for (int i = 0; i < _templateListModel.getSize(); i++)
252     {
253       _projectProps.addTemplate(_templateListModel.get(i).toString());
254     }
255     setVisible(false);
256
257   }
258
259   void _butAddTemplate_actionPerformed(ActionEvent JavaDoc e)
260   {
261     try
262     {
263       TemplateSet[] sets = _templateSetManager.getTemplateSets();
264       Vector JavaDoc addable = new Vector JavaDoc();
265
266       for (int i = 0; i < sets.length; i++)
267         addable.add(sets[i].getName());
268       Iterator JavaDoc it = _projectProps.getTemplateSetIterator();
269       while (it.hasNext())
270       {
271         addable.remove(it.next().toString());
272       }
273       Collections.sort(addable);
274       ListSelectionDlg seldlg = new ListSelectionDlg(this, "Add Template Set", addable);
275       if (!seldlg.wasCancelled())
276       {
277         Object JavaDoc[] sels = seldlg.getSelection();
278         for (int i = 0; i < sels.length; i++)
279         {
280           _projectProps.addTemplate(sels[i].toString());
281         }
282         updateTemplateListModel();
283         setTemplateSetSelection(sels);
284       }
285     }
286     catch (Exception JavaDoc x)
287     {
288       DjLogger.log(x);
289     }
290   }
291
292   protected void updateTemplateListModel()
293   {
294     Iterator JavaDoc ti = _projectProps.getTemplateSetIterator();
295     _templateListModel = new DefaultListModel JavaDoc();
296     while (ti.hasNext())
297       _templateListModel.addElement(ti.next());
298     _lstTemplates.setModel(_templateListModel);
299   }
300
301   protected void setTemplateSetSelection(Object JavaDoc[] sels)
302   {
303     int[] idxs = new int[sels.length];
304     Arrays.fill(idxs, -1);
305
306     int c = 0;
307     for (int i = 0; i < _templateListModel.getSize(); i++)
308     {
309       for (int j = 0; j < sels.length; j++)
310         if (_templateListModel.get(i).toString().equals(sels[j].toString())) idxs[c++] = i;
311     }
312     _lstTemplates.setSelectedIndices(idxs);
313   }
314
315   void _butRemoveTemplate_actionPerformed(ActionEvent JavaDoc e)
316   {
317     int[] idx = _lstTemplates.getSelectedIndices();
318
319     for (int i = 0; i < idx.length; i++)
320     {
321       _projectProps.removeTemplate(_templateListModel.get(idx[i]).toString());
322     }
323     updateTemplateListModel();
324   }
325
326 }
Popular Tags