KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > templateeditor > ui > TemplatePanel


1 package com.genimen.djeneric.tools.templateeditor.ui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import java.awt.event.MouseEvent JavaDoc;
7 import java.io.File JavaDoc;
8 import java.util.ArrayList JavaDoc;
9
10 import javax.swing.JButton JavaDoc;
11 import javax.swing.JFileChooser JavaDoc;
12 import javax.swing.JOptionPane JavaDoc;
13 import javax.swing.JPanel JavaDoc;
14 import javax.swing.JScrollPane JavaDoc;
15 import javax.swing.JTree JavaDoc;
16 import javax.swing.event.TreeExpansionEvent JavaDoc;
17 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
18 import javax.swing.tree.DefaultTreeModel JavaDoc;
19 import javax.swing.tree.ExpandVetoException JavaDoc;
20 import javax.swing.tree.TreePath JavaDoc;
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 JavaDoc
37 {
38   private static final long serialVersionUID = 1L;
39   public final static String JavaDoc TEMPLATESET_EXTENSION = "dts";
40
41   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
42   JScrollPane JavaDoc _scrTree = new JScrollPane JavaDoc();
43   JButton JavaDoc _butRescan = new JButton JavaDoc();
44   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
45   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
46   JPanel JavaDoc _pnlTemplates = new JPanel JavaDoc();
47   JTree JavaDoc _tree = new JTree JavaDoc();
48   JPanel JavaDoc _pnlButtons = new JPanel JavaDoc();
49   TemplateTreeCellRenderer _treeCellRendrer = new TemplateTreeCellRenderer();
50
51   TemplateTreeFolder _templateRootNode = new TemplateTreeFolder(null);
52   DefaultTreeModel JavaDoc _treeModel = new DefaultTreeModel JavaDoc(_templateRootNode);
53   DjPersistenceManager _mgr;
54   DjSession _session;
55   TemplateEditor _templateEditor;
56   TemplateSet _templateSet;
57   String JavaDoc _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 JavaDoc ex)
72     {
73       DjLogger.log(ex);
74     }
75   }
76
77   void jbInit() throws Exception JavaDoc
78   {
79     _tree.setCellRenderer(_treeCellRendrer);
80     this.setLayout(borderLayout1);
81     _butRescan.setPreferredSize(new Dimension JavaDoc(24, 24));
82     _butRescan.setToolTipText("Rescan template set");
83     _butRescan.setIcon(TemplateEditor.getImageIcon("refresh.gif"));
84     _butRescan.addActionListener(new java.awt.event.ActionListener JavaDoc()
85     {
86       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc()
101     {
102       public void mouseClicked(MouseEvent JavaDoc e)
103       {
104         _tree_mouseClicked(e);
105       }
106     });
107     _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener JavaDoc()
108     {
109       public void treeWillExpand(TreeExpansionEvent JavaDoc e) throws ExpandVetoException JavaDoc
110       {
111         _tree_treeWillExpand(e);
112       }
113
114       public void treeWillCollapse(TreeExpansionEvent JavaDoc 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 JavaDoc
143   {
144     if (_templateSetFileName == null) return saveTemplateSetAs();
145     else return doSaveTemplateSet();
146   }
147
148   protected boolean doSaveTemplateSet() throws Exception JavaDoc
149   {
150     _modified = false;
151     setStatusMessage("Template Set saved to " + _templateSetFileName);
152     return true;
153   }
154
155   protected void initFileChooser(JFileChooser JavaDoc fc)
156   {
157     fc.setCurrentDirectory(new File JavaDoc(_templateEditor.getProperty("filelocation", System.getProperties()
158         .getProperty("user.dir"))));
159   }
160
161   protected void recordFileChooserSettings(JFileChooser JavaDoc fc)
162   {
163     _templateEditor.setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath());
164   }
165
166   protected boolean saveTemplateSetAs() throws Exception JavaDoc
167   {
168     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
169     initFileChooser(fc);
170     fc.setDialogTitle("Save Template Set As");
171     DjFileFilter filter = new DjFileFilter(new String JavaDoc[]{TEMPLATESET_EXTENSION}, "TemplateSet");
172     fc.setFileFilter(filter);
173     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
174     if (_templateSetFileName != null)
175     {
176       fc.setSelectedFile(new File JavaDoc(_templateSetFileName));
177     }
178
179     if (fc.showSaveDialog(this) != JFileChooser.CANCEL_OPTION)
180     {
181       recordFileChooserSettings(fc);
182       String JavaDoc fileName = fc.getSelectedFile().getAbsolutePath();
183       if (!fileName.toLowerCase().endsWith("." + TEMPLATESET_EXTENSION))
184       {
185         fileName += "." + TEMPLATESET_EXTENSION;
186       }
187       File JavaDoc chk = new File JavaDoc(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 JavaDoc 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                                               // 0 1
201
, new String JavaDoc[]{"Overwrite", "Cancel"}, null);
202     return result == 0;
203   }
204
205   boolean isOkToAbandon() throws Exception JavaDoc
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                                                 // 0 1 2
212
, new String JavaDoc[]{"Save", "Discard", "Cancel"}, null);
213       if ((result != 0) && (result != 1))
214       {
215         return false;
216       }
217       // cancel or closed
218
if (result == 0)
219       {
220         return saveTemplateSet();
221       }
222     }
223     return true;
224   }
225
226   protected boolean canClose() throws Exception JavaDoc
227   {
228     return isOkToAbandon();
229   }
230
231   public void setStatusMessageNow(String JavaDoc msg, boolean isInformative)
232   {
233     _templateEditor.setStatusMessageNow(msg, isInformative);
234   }
235
236   public void setStatusMessage(String JavaDoc msg)
237   {
238     _templateEditor.setStatusMessage(msg, true);
239   }
240
241   public void setStatusMessage(Throwable JavaDoc t)
242   {
243     _templateEditor.setStatusMessage(t);
244   }
245
246   public void setStatusMessage(String JavaDoc 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 JavaDoc result = new ArrayList JavaDoc(10);
255
256     TreePath JavaDoc[] tp = _tree.getSelectionPaths();
257
258     for (int qq = 0; qq < tp.length; qq++)
259     {
260       DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) (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 JavaDoc e) throws ExpandVetoException JavaDoc
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 JavaDoc x)
278     {
279       setStatusMessage(x);
280       throw new ExpandVetoException JavaDoc(new TreeExpansionEvent JavaDoc(this, e.getPath()), x.getMessage());
281     }
282
283   }
284
285   void _butRescan_actionPerformed(ActionEvent JavaDoc 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 JavaDoc x)
300     {
301       setStatusMessage(x);
302     }
303   }
304
305   void _tree_mouseClicked(MouseEvent JavaDoc 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