1 30 package com.genimen.djeneric.tools.modeler.userperspective; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Color ; 34 import java.awt.Dimension ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 import java.io.BufferedInputStream ; 38 import java.io.File ; 39 import java.io.FileInputStream ; 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 43 import javax.swing.BorderFactory ; 44 import javax.swing.DefaultListModel ; 45 import javax.swing.JButton ; 46 import javax.swing.JFileChooser ; 47 import javax.swing.JList ; 48 import javax.swing.JOptionPane ; 49 import javax.swing.JPanel ; 50 import javax.swing.JScrollPane ; 51 import javax.swing.JSplitPane ; 52 import javax.swing.JTree ; 53 import javax.swing.border.BevelBorder ; 54 import javax.swing.border.Border ; 55 import javax.swing.event.TreeSelectionEvent ; 56 import javax.swing.tree.DefaultMutableTreeNode ; 57 import javax.swing.tree.DefaultTreeModel ; 58 import javax.swing.tree.TreePath ; 59 60 import com.genimen.djeneric.language.Messages; 61 import com.genimen.djeneric.repository.exceptions.DjenericException; 62 import com.genimen.djeneric.structure.ResourceDefinition; 63 import com.genimen.djeneric.tools.modeler.ModelEditor; 64 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 65 import com.genimen.djeneric.util.DjClassInspector; 66 import com.genimen.djeneric.util.DjFileFilter; 67 import com.genimen.djeneric.util.DjFileUtil; 68 import com.genimen.djeneric.util.DjLogger; 69 import com.genimen.djeneric.util.DjStringReplacer; 70 71 public class ResourceManagerPanel extends JPanel 72 { 73 private static final long serialVersionUID = 1L; 74 private static final String JARS_PATH = "/jars/"; 75 DefaultTreeModel _treeModel; 76 ViewEditor _viewEditor; 77 ModelEditor _editor; 78 ResourceFolderNode _root; 79 80 BorderLayout borderLayout1 = new BorderLayout (); 81 JSplitPane jSplitPane1 = new JSplitPane (); 82 JPanel jPanel1 = new JPanel (); 83 BorderLayout borderLayout2 = new BorderLayout (); 84 JScrollPane jScrollPane1 = new JScrollPane (); 85 JTree _tree = new JTree (); 86 JScrollPane jScrollPane2 = new JScrollPane (); 87 JList _lstContents = new JList (); 88 JPanel jPanel2 = new JPanel (); 89 JButton _butAdd = new JButton (); 90 JButton _butDelete = new JButton (); 91 BorderLayout borderLayout3 = new BorderLayout (); 92 ResourceFolderRenderer _renderer = new ResourceFolderRenderer(); 93 JPanel jPanel4 = new JPanel (); 94 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 95 JButton _butCreateFolder = new JButton (); 96 JButton _butSave = new JButton (); 97 JPanel jPanel3 = new JPanel (); 98 BorderLayout borderLayout4 = new BorderLayout (); 99 Border border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, 100 Color.white, new Color (109, 109, 101 110), new Color (156, 156, 158)); 102 103 public ResourceManagerPanel() 104 { 105 try 106 { 107 jbInit(); 108 _butAdd.setIcon(ModelEditor.getImageIcon("add.gif")); 109 _butSave.setIcon(ModelEditor.getImageIcon("save.gif")); 110 _butDelete.setIcon(ModelEditor.getImageIcon("remove.gif")); 111 _butCreateFolder.setIcon(ModelEditor.getImageIcon("addfolder.gif")); 112 _tree.putClientProperty("JTree.lineStyle", "Angled"); 113 _tree.setRootVisible(false); 114 _tree.setCellRenderer(_renderer); 115 _lstContents.setCellRenderer(new ResourceListRenderer()); 116 synchronize(); 117 } 118 catch (Exception ex) 119 { 120 DjLogger.log(ex); 121 } 122 } 123 124 protected void updateModel() 125 { 126 _root = new ResourceFolderNode(); 127 _treeModel = new DefaultTreeModel (_root); 128 _root.setModel(_treeModel); 129 _root.setTree(_tree); 130 _tree.setModel(_treeModel); 131 132 if (_viewEditor != null) 133 { 134 ResourceDefinition[] allDefs = _viewEditor.getResources(); 135 for (int i = 0; i < allDefs.length; i++) 136 { 137 if (!allDefs[i].isMarkedForDelete()) addToTree(allDefs[i]); 138 } 139 } 140 _treeModel.nodeStructureChanged(_root); 141 142 createFolder(JARS_PATH); 143 createFolder("/images/"); 144 145 _root.sortResources(); 146 } 147 148 protected ResourceDefinition addToTree(ResourceDefinition definition) 149 { 150 String path = definition.getPath(); 151 ResourceFolderNode folder = createOrRetrieveFolder(_root, path); 152 return folder.addOrReplaceResource(definition); 153 } 154 155 protected ResourceFolderNode createOrRetrieveFolder(ResourceFolderNode parent, String path) 156 { 157 int idx = path.indexOf("/"); 158 String name = path; 159 160 if (idx != -1) 161 { 162 name = path.substring(0, idx); 163 path = path.substring(idx + 1).trim(); 164 } 165 else path = ""; 166 167 if (name.length() == 0 && path.length() == 0) return parent; 168 169 if (name.trim().length() == 0) name = "/"; 170 171 ResourceFolderNode existing = parent.getChild(name); 172 ResourceFolderNode newNode; 173 174 if (existing != null) newNode = existing; 175 else 176 { 177 newNode = new ResourceFolderNode(_tree, _treeModel, name); 178 parent.add(newNode); 179 } 180 181 if (path.length() > 0) 182 { 183 return createOrRetrieveFolder(newNode, path); 184 } 185 return newNode; 186 } 187 188 194 public void setEditorReferences(ViewEditor viewEditor) 195 { 196 _editor = viewEditor.getModelEditor(); 197 _viewEditor = viewEditor; 198 } 199 200 void jbInit() throws Exception 201 { 202 this.setLayout(borderLayout1); 203 jPanel1.setLayout(borderLayout2); 204 _lstContents.setFont(new java.awt.Font ("Monospaced", 0, 12)); 205 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 206 { 207 public void valueChanged(TreeSelectionEvent e) 208 { 209 _tree_valueChanged(e); 210 } 211 }); 212 _butAdd.setPreferredSize(new Dimension (24, 24)); 213 _butAdd.setToolTipText(Messages.getString("ResourceManagerPanel.AddResource")); 214 _butAdd.addActionListener(new java.awt.event.ActionListener () 215 { 216 public void actionPerformed(ActionEvent e) 217 { 218 _butAdd_actionPerformed(e); 219 } 220 }); 221 _butDelete.setPreferredSize(new Dimension (24, 24)); 222 _butDelete.setToolTipText("Delete selected resource(s)"); 223 _butDelete.addActionListener(new java.awt.event.ActionListener () 224 { 225 public void actionPerformed(ActionEvent e) 226 { 227 butDelete_actionPerformed(e); 228 } 229 }); 230 _butCreateFolder.setPreferredSize(new Dimension (24, 24)); 231 _butCreateFolder.setToolTipText(Messages.getString("ResourceManagerPanel.CreateFolder")); 232 _butCreateFolder.addActionListener(new java.awt.event.ActionListener () 233 { 234 public void actionPerformed(ActionEvent e) 235 { 236 _butCreateFolder_actionPerformed(e); 237 } 238 }); 239 _butSave.setPreferredSize(new Dimension (24, 24)); 240 _butSave.setToolTipText(Messages.getString("ResourceManagerPanel.SaveSelectedResource")); 241 _butSave.addActionListener(new ActionListener () 242 { 243 public void actionPerformed(ActionEvent e) 244 { 245 _butSave_actionPerformed(e); 246 } 247 }); 248 249 jPanel2.setLayout(borderLayout3); 250 jPanel4.setLayout(verticalFlowLayout1); 251 jSplitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT); 252 jPanel3.setLayout(borderLayout4); 253 jPanel4.setBorder(border1); 254 this.add(jSplitPane1, BorderLayout.CENTER); 255 jPanel1.add(jScrollPane2, BorderLayout.CENTER); 256 jSplitPane1.add(jPanel3, JSplitPane.TOP); 257 jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER); 258 jScrollPane1.getViewport().add(_tree, null); 259 jScrollPane2.getViewport().add(_lstContents, null); 260 this.add(jPanel2, BorderLayout.SOUTH); 261 jPanel4.add(_butAdd, null); 262 jPanel4.add(_butDelete, null); 263 jPanel4.add(_butSave); 264 jPanel4.add(_butCreateFolder); 265 jSplitPane1.add(jPanel1, JSplitPane.BOTTOM); 266 jPanel3.add(jPanel4, java.awt.BorderLayout.WEST); 267 } 268 269 public ResourceFolderNode[] getSelectedNodes() 270 { 271 if (_tree.getSelectionPath() == null) return new ResourceFolderNode[0]; 272 ArrayList result = new ArrayList (10); 273 274 TreePath [] tp = _tree.getSelectionPaths(); 275 276 for (int qq = 0; qq < tp.length; qq++) 277 { 278 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (tp[qq].getLastPathComponent()); 279 if (node instanceof ResourceFolderNode) result.add(node); 280 } 281 return (ResourceFolderNode[]) result.toArray(new ResourceFolderNode[0]); 282 } 283 284 void _butAdd_actionPerformed(ActionEvent e) 285 { 286 ResourceFolderNode[] sels = getSelectedNodes(); 287 if (sels.length == 0) 288 { 289 JOptionPane.showMessageDialog(this, Messages.getString("ResourceManagerPanel.CreateSelectFolder"), Messages 290 .getString("ResourceManagerPanel.NoFolderSelected"), JOptionPane.INFORMATION_MESSAGE); 291 return; 292 } 293 294 JFileChooser fc = new JFileChooser (); 295 initFileChooser(fc); 296 DjFileFilter filter = new DjFileFilter(new String []{"gif", "class", "jar"}, Messages.getString("global.Resources")); 297 fc.setFileFilter(filter); 298 fc.setDialogTitle(Messages.getString("ResourceManagerPanel.LoadResource")); 299 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 300 fc.setMultiSelectionEnabled(true); 301 302 if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) 303 { 304 recordFileChooserSettings(fc); 305 try 306 { 307 File [] files = fc.getSelectedFiles(); 308 ArrayList added = new ArrayList (); 309 for (int i = 0; i < files.length; i++) 310 { 311 ResourceDefinition def = loadResource(sels[0], files[i].getAbsolutePath()); 312 if (def.getBytes().length > 20000) 313 { 314 if (JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(this, Messages 315 .getString("ResourceManagerPanel.LargeResource"), Messages 316 .getString("ResourceManagerPanel.LargeResourceTitle"), JOptionPane.YES_NO_OPTION, 317 JOptionPane.WARNING_MESSAGE)) 318 { 319 continue; 320 } 321 322 } 323 if (def.isJar()) 324 { 325 def.setPath(JARS_PATH); 326 } 327 added.add(def); 328 _viewEditor.addResource(def); 329 } 330 331 if (added.size() > 0) 333 { 334 ResourceDefinition res = (ResourceDefinition) added.get(0); 335 synchronize(); 336 ResourceFolderNode folder = createOrRetrieveFolder(_root, res.getPath()); 337 selectNode(folder); 338 selectResources((ResourceDefinition[]) added.toArray(new ResourceDefinition[0])); 339 _viewEditor.updateSelectedNodeData(); 340 } 341 } 342 catch (Exception e1) 343 { 344 DjLogger.log(e1); 345 } 346 } 347 348 } 349 350 protected void selectResources(ResourceDefinition[] definitions) 351 { 352 _lstContents.setSelectedIndex(-1); 353 int[] indices = new int[definitions.length]; 354 for (int x = 0; x < definitions.length; x++) 355 { 356 for (int i = 0; i < _lstContents.getModel().getSize(); i++) 357 { 358 if (_lstContents.getModel().getElementAt(i) == definitions[x]) 359 { 360 indices[x] = i; 361 } 362 } 363 _lstContents.setSelectedIndices(indices); 364 } 365 } 366 367 protected ResourceDefinition loadResource(ResourceFolderNode dest, String filename) throws IOException , 368 DjenericException 369 { 370 File infile = new File (filename); 371 long size = infile.length(); 372 373 FileInputStream fis = new FileInputStream (infile); 374 BufferedInputStream bis = new BufferedInputStream (fis); 375 376 byte[] buffer = new byte[(int) size]; 377 int offset = 0; 378 int totalRead = 0; 379 while (totalRead < size) 380 { 381 totalRead += bis.read(buffer, offset, (int) (size - totalRead)); 382 } 383 ResourceDefinition def = new ResourceDefinition(); 384 def.setBytes(buffer); 385 boolean pathIsSet = false; 386 if (filename.toLowerCase().endsWith(".class")) 387 { 388 DjClassInspector inspector = new DjClassInspector(buffer); 389 if (inspector.isValidClass()) 390 { 391 pathIsSet = true; 392 def.setAbsolutePath(inspector.getClassName() + ".class"); 393 } 394 } 395 if (!pathIsSet) 396 { 397 def.setAbsolutePath(filename); 398 def.setPath(dest.getPathString()); 399 } 400 bis.close(); 401 return dest.addOrReplaceResource(def); 402 } 403 404 protected void initFileChooser(JFileChooser fc) 405 { 406 fc 407 .setCurrentDirectory(new File (_editor.getProperty("filelocation", System.getProperties() 408 .getProperty("user.dir")))); 409 } 410 411 protected void recordFileChooserSettings(JFileChooser fc) 412 { 413 _editor.setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath()); 414 } 415 416 void _tree_valueChanged(TreeSelectionEvent e) 417 { 418 updateSelectedFolderData(); 419 } 420 421 public void updateSelectedFolderData() 422 { 423 DefaultListModel listModel = new DefaultListModel (); 424 425 _lstContents.setModel(listModel); 426 ResourceFolderNode[] sels = getSelectedNodes(); 427 428 if (sels.length > 0) 429 { 430 ResourceDefinition[] resources = sels[0].getResources(); 431 for (int i = 0; i < resources.length; i++) 432 { 433 listModel.addElement(resources[i]); 434 } 435 } 436 } 437 438 public void synchronize() 439 { 440 updateModel(); 441 updateSelectedFolderData(); 442 _root.expandAll(); 443 } 444 445 void butDelete_actionPerformed(ActionEvent e) 446 { 447 ResourceFolderNode[] sels = getSelectedNodes(); 448 if (sels.length == 0) return; 449 450 Object [] defs = _lstContents.getSelectedValues(); 451 boolean killAll = false; 452 for (int i = 0; i < defs.length; i++) 453 { 454 ResourceDefinition def = (ResourceDefinition) defs[i]; 455 int result = 0; 456 if (!killAll) 457 { 458 result = JOptionPane.showOptionDialog(this, Messages.getString("ResourceManagerPanel.Want2Delete", def 459 .getName()), Messages.getString("ResourceManagerPanel.DeleteResource"), JOptionPane.DEFAULT_OPTION, 460 JOptionPane.INFORMATION_MESSAGE, null, new String []{ 461 Messages.getString("global.Delete"), 462 Messages.getString("ResourceManagerPanel.All"), 463 Messages.getString("global.Cancel")}, null); 464 killAll = result == 1; 465 } 466 if (killAll || result == 0) 467 { 468 sels[0].removeResource(def); 469 def.markDeleted(true); 470 } 471 if (result == 2) 472 { 473 updateSelectedFolderData(); 474 _viewEditor.updateSelectedNodeData(); 475 return; 476 } 477 } 478 updateSelectedFolderData(); 479 _viewEditor.updateSelectedNodeData(); 480 } 481 482 public void selectNode(ResourceFolderNode node) 483 { 484 _tree.setSelectionPath(new TreePath (node.getPath())); 485 updateSelectedFolderData(); 486 } 487 488 private void createFolder(String txt) 489 { 490 txt = createValidPath(txt); 491 492 ResourceFolderNode node = createOrRetrieveFolder(_root, txt); 493 _treeModel.nodeStructureChanged(_root); 494 _root.expandAll(); 495 selectNode(node); 496 } 497 498 protected String createValidPath(String path) 499 { 500 if (!path.startsWith("/")) path = "/" + path; 501 502 DjStringReplacer sr = new DjStringReplacer(path); 503 sr.replace("\\", "/"); 504 sr.replace("//", "/"); 505 path = sr.toString(); 506 return path; 507 } 508 509 void _butCreateFolder_actionPerformed(ActionEvent e) 510 { 511 String path = ""; 512 ResourceFolderNode[] sels = getSelectedNodes(); 513 if (sels.length != 0) path = sels[0].getPathString() + "/"; 514 515 String folder = JOptionPane.showInputDialog(this, Messages.getString("ResourceManagerPanel.CreateFolder"), path); 516 if (folder != null && folder.trim().length() > 0) createFolder(folder); 517 } 518 519 public void _butSave_actionPerformed(ActionEvent e) 520 { 521 ResourceFolderNode[] sels = getSelectedNodes(); 522 if (sels.length == 0) return; 523 524 try 525 { 526 JFileChooser fc = new JFileChooser (); 527 fc.setDialogTitle(Messages.getString("ModelEditor.SaveResource")); 528 initFileChooser(fc); 529 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 530 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 531 { 532 String destpath = fc.getSelectedFile().getAbsolutePath(); 533 if (!destpath.endsWith("/") && !destpath.endsWith("\\")) destpath += "/"; 534 535 Object [] defs = _lstContents.getSelectedValues(); 536 for (int i = 0; i < defs.length; i++) 537 { 538 ResourceDefinition def = (ResourceDefinition) defs[i]; 539 540 byte[] bytes = def.getBytes(); 541 542 String dirname = destpath + def.getPath(); 543 DjFileUtil.createDirectory(dirname); 544 String filename = dirname + def.getName() + "." + def.getType(); 545 DjFileUtil.writeFile(filename, bytes); 546 new File (dirname + def.getName() + "." + def.getType()).setLastModified(def.getTime()); 547 } 548 _editor.setStatusMessage(Messages 549 .getString("ModelEditor.ResourcesSaved", String.valueOf(defs.length), destpath)); 550 } 551 } 552 catch (Exception e1) 553 { 554 _editor.setStatusMessage(e1); 555 } 556 } 557 558 } 559 | Popular Tags |