View Javadoc
1 package com.bonevich.erj.ui; 2 3 import com.bonevich.erj.ErjConstants; 4 import com.bonevich.erj.app.Application; 5 import com.bonevich.erj.app.Project; 6 import com.bonevich.erj.diagram.ERDiagram; 7 import com.bonevich.javax.swing.event.MenuAdapter; 8 import com.bonevich.util.FileHistory; 9 import com.bonevich.util.FileHistoryOwner; 10 import com.bonevich.util.dependency.Dependent; 11 import com.bonevich.util.dependency.UpdateAction; 12 import com.bonevich.util.dependency.VisualDependent; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.tigris.gef.base.Globals; 17 import org.tigris.gef.base.ModeSelect; 18 import org.tigris.gef.event.ModeChangeEvent; 19 import org.tigris.gef.event.ModeChangeListener; 20 import org.tigris.gef.ui.IStatusBar; 21 import org.tigris.gef.ui.ToolBar; 22 import org.tigris.gef.util.ResourceLoader; 23 24 import java.awt.BorderLayout; 25 import java.awt.Dimension; 26 import java.text.DateFormat; 27 import java.util.Date; 28 29 import javax.swing.*; 30 import javax.swing.border.Border; 31 import javax.swing.border.EtchedBorder; 32 import javax.swing.event.MenuEvent; 33 import javax.swing.event.MenuListener; 34 import javax.swing.tree.DefaultTreeModel; 35 36 /*** 37 * 38 * @author Jeffrey Bonevich <bonevich@telocity.com> 39 * @version 0.1 40 */ 41 public final class ErjFrame extends JFrame 42 implements IStatusBar, ModeChangeListener, 43 Cloneable, ErjConstants, FileHistoryOwner 44 { 45 private static final Log _logger = LogFactory.getLog(ErjFrame.class); 46 47 ////////////////////////////////////////////////////////// 48 // Attributes 49 private final static ErjFrame INSTANCE = 50 new ErjFrame("ERJ - A Relational Data Modeling Tool for Java"); 51 52 private Application _application; 53 54 private ERDiagramPane _diagramPane; 55 private JMenuBar _menuBar = new JMenuBar(); 56 private JMenu _fileMenu = new JMenu("File"); 57 private FileHistory _fileHistory = new FileHistory(this); 58 private ErjToolBar _mainBar = new ErjToolBar(); 59 private JSplitPane _splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 60 private JLabel _statusBar = new JLabel(" "); 61 private JLabel _modifiedLabel = new JLabel("*"); 62 private ErjTree _schemaTree; 63 private JScrollPane _treeView = new JScrollPane( 64 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 65 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS 66 ); 67 private ToolBar _toolBar; 68 { 69 /* 70 Seems to be causing a NPE in AWT, not set up correctly in GEF? 71 Localizer.addResource("GefBase","org.tigris.gef.base.BaseResourceBundle"); 72 Localizer.addResource("GefPres","org.tigris.gef.presentation.PresentationResourceBundle"); 73 Localizer.addLocale(Locale.getDefault()); 74 Localizer.switchCurrentLocale(Locale.getDefault()); 75 */ 76 ResourceLoader.addResourceExtension("gif"); 77 ResourceLoader.addResourceLocation("/org/tigris/gef/Images"); 78 ResourceLoader.addResourceLocation("/com/bonevich/erj/images"); 79 _toolBar = new ErjPalette("ERJ Toolbar"); 80 } 81 82 private Dependent _dep_projectModified; 83 84 /*** 85 * ErjFrame constructor. Private to enforce singleton pattern. 86 */ 87 private ErjFrame(String name) 88 { 89 super(name); 90 } 91 92 public static ErjFrame getInstance() 93 { 94 return INSTANCE; 95 } 96 97 public void init(Splash progress, int currentProgess) 98 { 99 initMenus(); 100 progress.showProgress(currentProgess + 10); 101 initMainToolBar(); 102 progress.showProgress(currentProgess + 20); 103 initContent(); 104 } 105 106 private void initMenus() 107 { 108 JMenu newMenu = new JMenu("New"); 109 newMenu.add(Command.NewProject); 110 newMenu.add(Command.NewDiagram); 111 112 _fileMenu.add(newMenu); 113 _fileMenu.add(Command.Open); 114 _fileMenu.addSeparator(); 115 _fileMenu.add(Command.Save); 116 _fileMenu.add(Command.SaveAs); 117 _fileMenu.addSeparator(); 118 _fileMenu.add(Command.Print); 119 _fileMenu.add(Command.PageSetup); 120 _fileMenu.addSeparator(); 121 _fileMenu.add(Command.Exit); 122 _fileMenu.setMnemonic('F'); 123 _menuBar.add(_fileMenu); 124 125 _fileHistory.initFileMenuHistory(); 126 127 JMenu edit = new JMenu("Edit"); 128 edit.add(Command.Undo); 129 edit.add(Command.Redo); 130 edit.addSeparator(); 131 edit.add(Command.Cut); 132 edit.add(Command.Copy); 133 edit.add(Command.Paste); 134 edit.add(Command.Delete); 135 edit.addSeparator(); 136 edit.add(Command.SelectAll); 137 edit.add(Command.Remove); 138 edit.addSeparator(); 139 edit.add(Command.History); 140 edit.add(Command.Prefs); 141 edit.setMnemonic('E'); 142 _menuBar.add(edit); 143 144 JMenu view = new JMenu("View"); 145 view.add(Command.Zoom); 146 view.addSeparator(); 147 view.add(Command.Goto); 148 view.addSeparator(); 149 view.add(Command.AdjustGrid); 150 view.add(Command.AdjustSnap); 151 view.add(Command.AdjustPageBreak); 152 view.setMnemonic('V'); 153 _menuBar.add(view); 154 155 JMenu project = new JMenu("Project"); 156 project.add(Command.ProjectProps); 157 project.add(Command.TargetDatabase); 158 project.setMnemonic('P'); 159 _menuBar.add(project); 160 161 JMenu tools = new JMenu("Tools"); 162 tools.add(Command.Generate); 163 tools.add(Command.Synchronize); 164 tools.addSeparator(); 165 tools.add(Command.Arrange); 166 tools.setMnemonic('T'); 167 _menuBar.add(tools); 168 169 JMenu help = new JMenu("Help"); 170 help.add(Command.Contents); 171 help.add(Command.About); 172 help.setMnemonic('H'); 173 _menuBar.add(help); 174 175 MenuListener menuListener = new MenuAdapter() 176 { 177 public void menuSelected(MenuEvent e) 178 { 179 Command.updateAllEnabled(); 180 } 181 }; 182 _fileMenu.addMenuListener(menuListener); 183 edit.addMenuListener(menuListener); 184 view.addMenuListener(menuListener); 185 project.addMenuListener(menuListener); 186 tools.addMenuListener(menuListener); 187 help.addMenuListener(menuListener); 188 189 setJMenuBar(_menuBar); 190 } 191 192 private void initMainToolBar() 193 { 194 JButton newButton = Command.NewProject.getButton(); 195 JButton openButton = Command.Open.getButton(); 196 JButton saveButton = Command.Save.getButton(); 197 JButton saveAsButton = Command.SaveAs.getButton(); 198 JButton printButton = Command.Print.getButton(); 199 JButton pageSetupButton = Command.PageSetup.getButton(); 200 JButton cutButton = Command.Cut.getButton(); 201 JButton copyButton = Command.Copy.getButton(); 202 JButton pasteButton = Command.Paste.getButton(); 203 JButton zoomInButton = Command.ZoomIn.getButton(); 204 JButton zoomOutButton = Command.ZoomOut.getButton(); 205 206 // Tool bar 207 _mainBar.add(newButton); 208 _mainBar.add(openButton); 209 _mainBar.add(saveButton); 210 _mainBar.add(saveAsButton); 211 _mainBar.addSeparator(); 212 _mainBar.add(printButton); 213 _mainBar.add(pageSetupButton); 214 _mainBar.addSeparator(); 215 _mainBar.add(cutButton); 216 _mainBar.add(copyButton); 217 _mainBar.add(pasteButton); 218 _mainBar.addSeparator(); 219 _mainBar.add(zoomInButton); 220 _mainBar.add(zoomOutButton); 221 _mainBar.addSeparator(); 222 223 _mainBar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); 224 225 JPanel toolBarPane = new JPanel(); 226 toolBarPane.setLayout(new BoxLayout(toolBarPane, BoxLayout.X_AXIS)); 227 toolBarPane.add(_mainBar); 228 toolBarPane.add(_toolBar); 229 toolBarPane.add(Box.createRigidArea(new Dimension(1,8))); 230 getContentPane().add(toolBarPane, java.awt.BorderLayout.NORTH); 231 } 232 233 private void initContent() 234 { 235 _splitPane.setOneTouchExpandable(true); 236 _splitPane.setLeftComponent(_treeView); 237 _splitPane.setRightComponent(_diagramPane); // does nothing - even after diagramPane is initialized 238 getContentPane().add(_splitPane, BorderLayout.CENTER); 239 240 // status bar 241 JPanel statusPane = new JPanel(); 242 statusPane.setLayout(new BoxLayout(statusPane, BoxLayout.X_AXIS)); 243 statusPane.setBorder(new EtchedBorder(EtchedBorder.RAISED)); 244 statusPane.add(_statusBar); 245 246 _modifiedLabel = new JLabel(); 247 JLabel datetimeLabel = new JLabel(DateFormat.getDateTimeInstance().format(new Date(System.currentTimeMillis()))); 248 Border brdr = new EtchedBorder(EtchedBorder.LOWERED); 249 _modifiedLabel.setBorder(brdr); 250 datetimeLabel.setBorder(brdr); 251 _modifiedLabel.setFont(STATUSBAR_FONT); 252 _modifiedLabel.setForeground(STATUSBAR_MOD_FILL); 253 datetimeLabel.setFont(STATUSBAR_FONT_ITALIC); 254 datetimeLabel.setForeground(STATUSBAR_COLOR); 255 statusPane.add(Box.createHorizontalGlue()); 256 statusPane.add(_modifiedLabel); 257 statusPane.add(datetimeLabel); 258 259 getContentPane().add(statusPane, BorderLayout.SOUTH); 260 261 // the following allows me to cancel closing the window 262 // if the user wishes to cancel or save 263 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 264 addWindowListener( 265 new java.awt.event.WindowAdapter() 266 { 267 public void windowClosing(java.awt.event.WindowEvent e) 268 { 269 Command.Exit.invoke(); 270 } 271 } 272 ); 273 } 274 275 public void setupProjectResources(Project project) 276 { 277 if (_logger.isDebugEnabled()) 278 { 279 _logger.debug("setting up project resources..."); 280 } 281 disposeOfProjectResources(); 282 283 _diagramPane = new ERDiagramPane(project); 284 _splitPane.setRightComponent(_diagramPane); 285 286 // Tree view 287 ErjTreeNode rootNode = new ErjTreeNode(project.getModel()); 288 //ErjTreeModel treeModel = new ErjTreeModel(rootNode); 289 DefaultTreeModel treeModel = new DefaultTreeModel(rootNode); 290 _schemaTree = new ErjTree(treeModel); 291 _schemaTree.addTreeSelectionListener(_mainBar); 292 _treeView.setViewportView(_schemaTree); 293 _splitPane.setDividerLocation(INITIAL_FRAME_SIZE.width / 3); 294 295 initDependents(project); 296 } 297 298 private void initDependents(final Project project) 299 { 300 _dep_projectModified = new VisualDependent( 301 new UpdateAction() 302 { 303 public void onUpdate() 304 { 305 if (project != null && project.isModified()) 306 { 307 _modifiedLabel.setText("*"); 308 } 309 else 310 { 311 _modifiedLabel.setText(" "); 312 } 313 } 314 } 315 ); 316 } 317 318 private void disposeOfProjectResources() 319 { 320 _diagramPane = null; 321 _schemaTree = null; 322 } 323 324 public FileHistory getFileHistory() 325 { 326 return _fileHistory; 327 } 328 329 public ErjToolBar getMainToolBar() 330 { 331 return _mainBar; 332 } 333 334 public ERDiagram getActiveDiagram() 335 { 336 return _diagramPane.getSelectedDiagram(); 337 } 338 339 public void deleteActiveDiagram() 340 { 341 ERDiagram d = getActiveDiagram(); 342 _application.getCurrentProject().removeDiagram(d); 343 _diagramPane.removeDiagram(d); 344 } 345 346 /*** 347 * Dispose of this frame, save the file history to disk, and exit 348 * the application 349 */ 350 public void exit() 351 { 352 dispose(); 353 _fileHistory.saveHistoryEntries(); 354 System.exit(0); 355 } 356 357 ////////////////////////////////////////////////////////// 358 // IStatusBar implementation 359 /*** 360 * Display a status message in the status bar 361 */ 362 public void showStatus(String msg) 363 { 364 if (_statusBar != null) 365 { 366 _statusBar.setText(msg); 367 } 368 } 369 370 /*** 371 * Set status bar in Globals if we are visible 372 */ 373 public void setVisible(boolean isVisible) 374 { 375 super.setVisible(isVisible); 376 if (isVisible) 377 { 378 Globals.setStatusBar(this); 379 } 380 } 381 382 //////////////////////////////////////////////////////////////// 383 // ModeChangeListener implementation 384 public void modeChange(ModeChangeEvent e) 385 { 386 if (!Globals.getSticky() && Globals.mode() instanceof ModeSelect) 387 { 388 _toolBar.unpressAllButtons(); 389 } 390 } 391 392 //////////////////////////////////////////////////////////////// 393 // FileHistoryOwner implementation 394 public String getApplicationName() 395 { 396 return "erj"; 397 } 398 399 public JMenu getFileMenu() 400 { 401 return _fileMenu; 402 } 403 404 public JFrame getFrame() 405 { 406 return this; 407 } 408 409 public void loadFile(String pathname) 410 { 411 Command opener = new Command.CommandOpenFromHistory(pathname); 412 opener.invoke(); 413 } 414 415 /*** 416 * Returns the application. 417 * @return Application 418 */ 419 public Application getApplication() 420 { 421 return _application; 422 } 423 424 /*** 425 * Sets the application. 426 * @param application The application to set 427 */ 428 public void setApplication(Application application) 429 { 430 _application = application; 431 } 432 433 } /*** end class ErjFrame */

This page was automatically generated by Maven