1 package com.genimen.djeneric.tools.templateeditor.ui; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Dimension ; 5 import java.awt.event.ActionEvent ; 6 import java.awt.event.MouseEvent ; 7 import java.io.File ; 8 import java.util.ArrayList ; 9 10 import javax.swing.JButton ; 11 import javax.swing.JFileChooser ; 12 import javax.swing.JOptionPane ; 13 import javax.swing.JPanel ; 14 import javax.swing.JScrollPane ; 15 import javax.swing.JTree ; 16 import javax.swing.event.TreeExpansionEvent ; 17 import javax.swing.tree.DefaultMutableTreeNode ; 18 import javax.swing.tree.DefaultTreeModel ; 19 import javax.swing.tree.ExpandVetoException ; 20 import javax.swing.tree.TreePath ; 21 22 import com.genimen.djeneric.repository.DjPersistenceManager; 23 import com.genimen.djeneric.repository.DjSession; 24 import com.genimen.djeneric.structure.ExtentUsage; 25 import com.genimen.djeneric.tools.generator.util.Template; 26 import com.genimen.djeneric.tools.generator.util.TemplateSet; 27 import com.genimen.djeneric.tools.templateeditor.TemplateEditor; 28 import com.genimen.djeneric.tools.templateeditor.tree.DefaultTemplateTreeNode; 29 import com.genimen.djeneric.tools.templateeditor.tree.TemplateTreeCellRenderer; 30 import com.genimen.djeneric.tools.templateeditor.tree.TemplateTreeFolder; 31 import com.genimen.djeneric.tools.templateeditor.tree.TemplateTreeNode; 32 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 33 import com.genimen.djeneric.util.DjFileFilter; 34 import com.genimen.djeneric.util.DjLogger; 35 36 public class TemplatePanel extends JPanel 37 { 38 private static final long serialVersionUID = 1L; 39 public final static String TEMPLATESET_EXTENSION = "dts"; 40 41 BorderLayout borderLayout1 = new BorderLayout (); 42 JScrollPane _scrTree = new JScrollPane (); 43 JButton _butRescan = new JButton (); 44 BorderLayout borderLayout4 = new BorderLayout (); 45 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 46 JPanel _pnlTemplates = new JPanel (); 47 JTree _tree = new JTree (); 48 JPanel _pnlButtons = new JPanel (); 49 TemplateTreeCellRenderer _treeCellRendrer = new TemplateTreeCellRenderer(); 50 51 TemplateTreeFolder _templateRootNode = new TemplateTreeFolder(null); 52 DefaultTreeModel _treeModel = new DefaultTreeModel (_templateRootNode); 53 DjPersistenceManager _mgr; 54 DjSession _session; 55 TemplateEditor _templateEditor; 56 TemplateSet _templateSet; 57 String _templateSetFileName = null; 58 boolean _modified = false; 59 60 public TemplatePanel(TemplateEditor templateEditor, TemplateSet templateSet) 61 { 62 try 63 { 64 _mgr = templateEditor.getManager(); 65 _session = templateEditor.getSession(); 66 _templateEditor = templateEditor; 67 _templateSet = templateSet; 68 jbInit(); 69 setupNavigator(templateEditor.getNavigatorStructure()); 70 } 71 catch (Exception ex) 72 { 73 DjLogger.log(ex); 74 } 75 } 76 77 void jbInit() throws Exception 78 { 79 _tree.setCellRenderer(_treeCellRendrer); 80 this.setLayout(borderLayout1); 81 _butRescan.setPreferredSize(new Dimension (24, 24)); 82 _butRescan.setToolTipText("Rescan template set"); 83 _butRescan.setIcon(TemplateEditor.getImageIcon("refresh.gif")); 84 _butRescan.addActionListener(new java.awt.event.ActionListener () 85 { 86 public void actionPerformed(ActionEvent e) 87 { 88 _butRescan_actionPerformed(e); 89 } 90 }); 91 92 _pnlTemplates.setLayout(borderLayout4); 93 _tree.setRootVisible(false); 94 _tree.setCellRenderer(_treeCellRendrer); 95 _tree.setModel(_treeModel); 96 _tree.setShowsRootHandles(true); 97 _tree.setAutoscrolls(true); 98 _tree.setDoubleBuffered(true); 99 _tree.setToggleClickCount(0); 100 _tree.addMouseListener(new java.awt.event.MouseAdapter () 101 { 102 public void mouseClicked(MouseEvent e) 103 { 104 _tree_mouseClicked(e); 105 } 106 }); 107 _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener () 108 { 109 public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 110 { 111 _tree_treeWillExpand(e); 112 } 113 114 public void treeWillCollapse(TreeExpansionEvent e) 115 { 116 } 117 }); 118 _tree.setCellRenderer(_treeCellRendrer); 119 _pnlButtons.setLayout(verticalFlowLayout2); 120 _tree.putClientProperty("JTree.lineStyle", "Angled"); 121 _pnlTemplates.add(_scrTree, BorderLayout.CENTER); 122 _pnlTemplates.add(_pnlButtons, BorderLayout.WEST); 123 _pnlButtons.add(_butRescan, null); 124 _scrTree.getViewport().add(_tree); 125 this.add(_pnlTemplates, BorderLayout.CENTER); 126 } 127 128 void setupNavigator(ExtentUsage[] navigatorStructure) 129 { 130 _templateRootNode.setTree(_tree); 131 _templateRootNode.setTemplateSet(_templateSet); 132 _templateRootNode.removeAllChildren(); 133 134 for (int i = 0; i < navigatorStructure.length; i++) 135 { 136 TemplateTreeFolder ttn = new TemplateTreeFolder(navigatorStructure[i], _tree, _templateSet); 137 _templateRootNode.insertAsFolder(ttn); 138 } 139 _treeModel.nodeStructureChanged(_templateRootNode); 140 } 141 142 protected boolean saveTemplateSet() throws Exception 143 { 144 if (_templateSetFileName == null) return saveTemplateSetAs(); 145 else return doSaveTemplateSet(); 146 } 147 148 protected boolean doSaveTemplateSet() throws Exception 149 { 150 _modified = false; 151 setStatusMessage("Template Set saved to " + _templateSetFileName); 152 return true; 153 } 154 155 protected void initFileChooser(JFileChooser fc) 156 { 157 fc.setCurrentDirectory(new File (_templateEditor.getProperty("filelocation", System.getProperties() 158 .getProperty("user.dir")))); 159 } 160 161 protected void recordFileChooserSettings(JFileChooser fc) 162 { 163 _templateEditor.setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath()); 164 } 165 166 protected boolean saveTemplateSetAs() throws Exception 167 { 168 JFileChooser fc = new JFileChooser (); 169 initFileChooser(fc); 170 fc.setDialogTitle("Save Template Set As"); 171 DjFileFilter filter = new DjFileFilter(new String []{TEMPLATESET_EXTENSION}, "TemplateSet"); 172 fc.setFileFilter(filter); 173 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 174 if (_templateSetFileName != null) 175 { 176 fc.setSelectedFile(new File (_templateSetFileName)); 177 } 178 179 if (fc.showSaveDialog(this) != JFileChooser.CANCEL_OPTION) 180 { 181 recordFileChooserSettings(fc); 182 String fileName = fc.getSelectedFile().getAbsolutePath(); 183 if (!fileName.toLowerCase().endsWith("." + TEMPLATESET_EXTENSION)) 184 { 185 fileName += "." + TEMPLATESET_EXTENSION; 186 } 187 File chk = new File (fileName); 188 if (chk.exists() && !isOkToOverwrite(chk)) return false; 189 _templateSetFileName = fileName; 190 return doSaveTemplateSet(); 191 } 192 return false; 193 } 194 195 public boolean isOkToOverwrite(File file) 196 { 197 int result = JOptionPane.showOptionDialog(this, "Do you want to overwrite " + file.getAbsolutePath() + "?", 198 "File exists", JOptionPane.DEFAULT_OPTION, 199 JOptionPane.INFORMATION_MESSAGE, null 200 , new String []{"Overwrite", "Cancel"}, null); 202 return result == 0; 203 } 204 205 boolean isOkToAbandon() throws Exception 206 { 207 if (_modified) 208 { 209 int result = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Changes made", 210 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null 211 , new String []{"Save", "Discard", "Cancel"}, null); 213 if ((result != 0) && (result != 1)) 214 { 215 return false; 216 } 217 if (result == 0) 219 { 220 return saveTemplateSet(); 221 } 222 } 223 return true; 224 } 225 226 protected boolean canClose() throws Exception 227 { 228 return isOkToAbandon(); 229 } 230 231 public void setStatusMessageNow(String msg, boolean isInformative) 232 { 233 _templateEditor.setStatusMessageNow(msg, isInformative); 234 } 235 236 public void setStatusMessage(String msg) 237 { 238 _templateEditor.setStatusMessage(msg, true); 239 } 240 241 public void setStatusMessage(Throwable t) 242 { 243 _templateEditor.setStatusMessage(t); 244 } 245 246 public void setStatusMessage(String msg, boolean isInformative) 247 { 248 _templateEditor.setStatusMessage(msg, isInformative); 249 } 250 251 public DefaultTemplateTreeNode[] getSelectedNodes() 252 { 253 if (_tree.getSelectionPath() == null) return new DefaultTemplateTreeNode[0]; 254 ArrayList result = new ArrayList (10); 255 256 TreePath [] tp = _tree.getSelectionPaths(); 257 258 for (int qq = 0; qq < tp.length; qq++) 259 { 260 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (tp[qq].getLastPathComponent()); 261 if (node instanceof DefaultTemplateTreeNode) result.add(node); 262 } 263 return (DefaultTemplateTreeNode[]) result.toArray(new DefaultTemplateTreeNode[0]); 264 } 265 266 void _tree_treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 267 { 268 try 269 { 270 if (e.getPath().getLastPathComponent() instanceof DefaultTemplateTreeNode) 271 { 272 DefaultTemplateTreeNode node = (DefaultTemplateTreeNode) e.getPath().getLastPathComponent(); 273 node.expandNode(); 274 } 275 setStatusMessage(TemplateEditor.OK_MSG); 276 } 277 catch (Exception x) 278 { 279 setStatusMessage(x); 280 throw new ExpandVetoException (new TreeExpansionEvent (this, e.getPath()), x.getMessage()); 281 } 282 283 } 284 285 void _butRescan_actionPerformed(ActionEvent e) 286 { 287 try 288 { 289 for (int i = 0; i < _templateRootNode.getChildCount(); i++) 290 { 291 if (_templateRootNode.getChildAt(i) instanceof DefaultTemplateTreeNode) 292 { 293 DefaultTemplateTreeNode dttn = (DefaultTemplateTreeNode) _templateRootNode.getChildAt(i); 294 dttn.reload(); 295 } 296 } 297 setStatusMessage(TemplateEditor.OK_MSG); 298 } 299 catch (Exception x) 300 { 301 setStatusMessage(x); 302 } 303 } 304 305 void _tree_mouseClicked(MouseEvent e) 306 { 307 if (e.getClickCount() > 1) 308 { 309 editTemplate(); 310 } 311 } 312 313 private void editTemplate() 314 { 315 DefaultTemplateTreeNode[] sels = getSelectedNodes(); 316 for (int i = 0; i < sels.length; i++) 317 { 318 if (sels[i] instanceof TemplateTreeNode) 319 { 320 TemplateTreeNode ttn = (TemplateTreeNode) sels[i]; 321 Template tpl = ttn.getTemplate(); 322 DjLogger.log(tpl.getLocation()); 323 } 324 325 } 326 } 327 328 } | Popular Tags |