1 package com.genimen.djeneric.tools.generator.dialogs; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Dimension ; 5 import java.awt.Frame ; 6 import java.awt.Toolkit ; 7 import java.awt.event.ActionEvent ; 8 import java.io.File ; 9 import java.util.Arrays ; 10 import java.util.Collections ; 11 import java.util.Iterator ; 12 import java.util.Vector ; 13 14 import javax.swing.DefaultComboBoxModel ; 15 import javax.swing.DefaultListModel ; 16 import javax.swing.JButton ; 17 import javax.swing.JCheckBox ; 18 import javax.swing.JComboBox ; 19 import javax.swing.JDialog ; 20 import javax.swing.JFileChooser ; 21 import javax.swing.JLabel ; 22 import javax.swing.JList ; 23 import javax.swing.JPanel ; 24 import javax.swing.JScrollPane ; 25 import javax.swing.JTabbedPane ; 26 import javax.swing.JTable ; 27 import javax.swing.JTextField ; 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 36 { 37 private static final long serialVersionUID = 1L; 38 JPanel panel1 = new JPanel (); 39 BorderLayout borderLayout1 = new BorderLayout (); 40 JPanel jPanel2 = new JPanel (); 41 BorderLayout borderLayout2 = new BorderLayout (); 42 JPanel jPanel3 = new JPanel (); 43 JButton _butOk = new JButton (); 44 JButton _butCancel = new JButton (); 45 JTabbedPane _tabs = new JTabbedPane (); 46 JPanel _pnlGeneral = new JPanel (); 47 BorderLayout borderLayout3 = new BorderLayout (); 48 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 49 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 50 JPanel jPanel5 = new JPanel (); 51 JPanel jPanel4 = new JPanel (); 52 JLabel jLabel1 = new JLabel (); 53 JTextField _edtOutputPath = new JTextField (); 54 BorderLayout borderLayout4 = new BorderLayout (); 55 JPanel jPanel6 = new JPanel (); 56 JButton _butOutputPath = new JButton (); 57 JPanel _pnlTemplates = new JPanel (); 58 BorderLayout borderLayout5 = new BorderLayout (); 59 JScrollPane jScrollPane1 = new JScrollPane (); 60 JList _lstTemplates = new JList (); 61 JPanel jPanel1 = new JPanel (); 62 DjVerticalFlowLayout verticalFlowLayout3 = new DjVerticalFlowLayout(); 63 JButton _butAddTemplate = new JButton (); 64 JButton _butRemoveTemplate = new JButton (); 65 JPanel _pnlDefines = new JPanel (); 66 JScrollPane jScrollPane2 = new JScrollPane (); 67 BorderLayout borderLayout6 = new BorderLayout (); 68 JTable jTable1 = new JTable (); 69 JPanel jPanel7 = new JPanel (); 70 DjVerticalFlowLayout verticalFlowLayout4 = new DjVerticalFlowLayout(); 71 JButton _butAddDefine = new JButton (); 72 JButton _butRemoveDefine = new JButton (); 73 boolean _canceled = true; 74 ProjectProperties _projectProps; 75 DefaultListModel _templateListModel = new DefaultListModel (); 76 TemplateSetManager _templateSetManager; 77 JLabel jLabel2 = new JLabel (); 78 JComboBox _cbbOverwrite = new JComboBox (); 79 80 String [] _overwriteOptions = new String []{"Ask", "Always", "Never"}; 81 DefaultComboBoxModel _cbbModel = new DefaultComboBoxModel (_overwriteOptions); 82 JLabel jLabel3 = new JLabel (); 83 JCheckBox _chkOpenAfterGenerate = new JCheckBox (); 84 85 public ProjectPropertiesDialog(Frame frame, String 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 screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 99 Dimension 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 ti = _projectProps.getTemplateSetIterator(); 106 while (ti.hasNext()) 107 _templateListModel.addElement(ti.next()); 108 109 112 if (_templateListModel.getSize() > 0) _lstTemplates.setSelectedIndex(0); 113 setVisible(true); 114 } 115 catch (Exception ex) 116 { 117 DjLogger.log(ex); 118 } 119 } 120 121 void jbInit() throws Exception 122 { 123 panel1.setLayout(borderLayout1); 124 jPanel2.setLayout(borderLayout2); 125 _butOk.setPreferredSize(new Dimension (73, 27)); 126 _butOk.setText("OK"); 127 _butOk.addActionListener(new java.awt.event.ActionListener () 128 { 129 public void actionPerformed(ActionEvent e) 130 { 131 _butOk_actionPerformed(e); 132 } 133 }); 134 _butCancel.setText("Cancel"); 135 _butCancel.addActionListener(new java.awt.event.ActionListener () 136 { 137 public void actionPerformed(ActionEvent 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 (66, 21)); 146 jLabel1.setText("Output path"); 147 jPanel6.setLayout(borderLayout4); 148 _butOutputPath.setText("..."); 149 _butOutputPath.addActionListener(new java.awt.event.ActionListener () 150 { 151 public void actionPerformed(ActionEvent e) 152 { 153 _butOutputPath_actionPerformed(e); 154 } 155 }); 156 _butOutputPath.setPreferredSize(new Dimension (21, 21)); 157 _pnlTemplates.setLayout(borderLayout5); 158 jPanel1.setLayout(verticalFlowLayout3); 159 _butAddTemplate.setText("Add"); 160 _butAddTemplate.addActionListener(new java.awt.event.ActionListener () 161 { 162 public void actionPerformed(ActionEvent e) 163 { 164 _butAddTemplate_actionPerformed(e); 165 } 166 }); 167 _butRemoveTemplate.setText("Remove"); 168 _butRemoveTemplate.addActionListener(new java.awt.event.ActionListener () 169 { 170 public void actionPerformed(ActionEvent 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 (56, 21)); 181 jLabel2.setText("Overwrite"); 182 _cbbOverwrite.setPreferredSize(new Dimension (130, 21)); 183 _cbbOverwrite.setModel(_cbbModel); 184 jLabel3.setPreferredSize(new Dimension (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 e) 224 { 225 JFileChooser fc = new JFileChooser (); 226 fc.setDialogTitle("Generate to path"); 227 String path = _edtOutputPath.getText(); 228 if (path.trim().length() == 0) path = System.getProperties().getProperty("user.dir"); 229 fc.setCurrentDirectory(new File (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 e) 239 { 240 setVisible(false); 241 } 242 243 void _butOk_actionPerformed(ActionEvent 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 e) 260 { 261 try 262 { 263 TemplateSet[] sets = _templateSetManager.getTemplateSets(); 264 Vector addable = new Vector (); 265 266 for (int i = 0; i < sets.length; i++) 267 addable.add(sets[i].getName()); 268 Iterator 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 [] 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 x) 287 { 288 DjLogger.log(x); 289 } 290 } 291 292 protected void updateTemplateListModel() 293 { 294 Iterator ti = _projectProps.getTemplateSetIterator(); 295 _templateListModel = new DefaultListModel (); 296 while (ti.hasNext()) 297 _templateListModel.addElement(ti.next()); 298 _lstTemplates.setModel(_templateListModel); 299 } 300 301 protected void setTemplateSetSelection(Object [] 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 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 |