import java.awt.event.*; import java.awt.*; import java.beans.*; import java.util.*; import java.net.*; import java.io.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.plaf.*; public class EAT_Desktop extends JFrame implements ActionListener, VetoableChangeListener { private JDesktopPane desktop; private JMenuItem openItem, exitItem, nextItem, cascadeItem, tileItem, short_File, previewRevFile2; private JMenuItem newform, previewFile, previewRevFile, openCal, about, openPP, runPing; private JCheckBoxMenuItem dragItem; private JButton winButton, javaButton, motifButton; String WOR_DIR = "./wor", if_wor, savestg = "save"; JFileChooser fc = new JFileChooser(WOR_DIR); JFileChooser fcr = new JFileChooser(WOR_DIR); WOR_Html_Filter filter = new WOR_Html_Filter(); WOR_Filter wor_filter = new WOR_Filter(); WOR_Rev_Filter rev_filter = new WOR_Rev_Filter(); static final Integer DOCLAYER = new Integer(5); static final Integer TOOLLAYER = new Integer(6); static final Integer HELPLAYER = new Integer(7); int nextFrameX = 0; int nextFrameY = 0; int frameDistance = 0; static final String ABOUTMSG = "EAT - Engineering Application Tool \n \nWritten by Lawrence L. Jett, Jr.\n"; public EAT_Desktop() { super( "EAT - Engineering Application Tool" ); final int inset = 10; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds ( inset, inset/2, screenSize.width - inset*2, screenSize.height - inset*2 ); // Set content pane of the JFrame to JDesktopPane desktop = new JDesktopPane(); getContentPane().add( desktop ); buildMenus(); buildLaF(); show(); } protected void buildLaF() { // Add the look and feel controls using regular AWT buttons JPanel lnfPanel = new JPanel(); LnFListener lnfListener = new LnFListener(this); javaButton = new JButton("Metal"); javaButton.addActionListener(lnfListener); lnfPanel.add(javaButton); motifButton = new JButton("Motif"); motifButton.addActionListener(lnfListener); lnfPanel.add(motifButton); winButton = new JButton("Windows"); winButton.addActionListener(lnfListener); lnfPanel.add(winButton); getContentPane().add(lnfPanel, BorderLayout.SOUTH); UIManager.addPropertyChangeListener(new UISwitchListener((JComponent)getRootPane())); } protected void buildMenus() { JMenuBar menuBar = new JMenuBar(); setJMenuBar( menuBar ); JMenu file = buildfileMenu(); JMenu wind = buildwindMenu(); JMenu form = buildFormMenu(); JMenu help = buildHelpMenu(); menuBar.add(file); menuBar.add(wind); menuBar.add(form); menuBar.add(help); } protected JMenu buildfileMenu() { JMenu file = new JMenu("File"); openItem = new JMenuItem( "Open File" ); openCal = new JMenuItem( "Open Calendar" ); openPP = new JMenuItem("Open Projects Page"); runPing = new JMenuItem( "Ping 24.207.159.20" ); exitItem = new JMenuItem( "Exit" ); openItem.addActionListener(this); openCal.addActionListener(this); openPP.addActionListener(this); runPing.addActionListener(this); exitItem.addActionListener(this); file.add(openItem); file.add(openCal); file.add(openPP); //file.add(runPing); file.add(exitItem); return file; } protected JMenu buildwindMenu() { JMenu wind = new JMenu( "Window" ); nextItem = new JMenuItem( "Next" ); cascadeItem = new JMenuItem( "Cascade" ); tileItem = new JMenuItem( "Tile" ); dragItem = new JCheckBoxMenuItem( "Drag Outline" ); nextItem.addActionListener(this); cascadeItem.addActionListener(this); tileItem.addActionListener(this); dragItem.addActionListener(this); wind.add(nextItem); wind.add(cascadeItem); wind.add(tileItem); wind.add(dragItem); return wind; } protected JMenu buildFormMenu() { JMenu form = new JMenu("Forms"); newform = new JMenuItem("Create New Work Order Request Form"); previewFile = new JMenuItem("Open Existing Work Order Request Form"); previewRevFile = new JMenuItem("Open Existing Work Order Request HISTORY Form"); previewRevFile2 = new JMenuItem("Open Existing Work Order Request HISTORY Form 2"); newform.addActionListener(this); previewFile.addActionListener(this); previewRevFile.addActionListener(this); previewRevFile2.addActionListener(this); form.add(newform); form.add(previewFile); form.add(previewRevFile); //form.add(previewRevFile2); return form; } protected JMenu buildHelpMenu() { JMenu help = new JMenu("Help"); about = new JMenuItem("About EAT..."); about.addActionListener(this); help.add(about); return help; } public void selectNextWindow() { JInternalFrame[] frames = desktop.getAllFrames(); for ( int i = 0; i < frames.length; i++ ) { if ( frames[i].isSelected() ) { try { int next = i + 1; while ( next != i && frames[next].isIcon() ) { next++; } if ( next == i ) { return; } frames[next].setSelected( true ); frames[next].toFront(); return; } catch( PropertyVetoException e ) {} } } } public void tileWindows() { JInternalFrame[] frames = desktop.getAllFrames(); // count frames that are not iconized int frameCount = 0; for ( int i = 0; i < frames.length; i++ ) { if ( !frames[i].isIcon() ) { frameCount++; } } int row = (int)Math.sqrt( frameCount ); int col = frameCount / row; int extra = frameCount % row; int width = desktop.getWidth() / col; int height = desktop.getHeight() / row; int r = 0; int c = 0; for ( int i = 0; i < frames.length; i++ ) { if ( !frames[i].isIcon() ) { try { frames[i].setMaximum( false ); frames[i].reshape( c * width, r * height, width, height ); r++; if ( r == row ) { r = 0; c++; if ( c == col - extra ) { row++; height = desktop.getHeight() / row; } } } catch( PropertyVetoException e ) {} } } } public void cascadeWindows() { JInternalFrame[] frames = desktop.getAllFrames(); int x = 0; int y = 0; int width = desktop.getWidth() / 2; int height = desktop.getHeight() / 2; for ( int i = 0; i < frames.length; i++ ) { if ( !frames[i].isIcon() ) { try { frames[i].setMaximum( false ); frames[i].reshape( x, y, width, height ); x += frameDistance; y += frameDistance; if ( x + width > desktop.getWidth() ) x = 0; if ( y + height > desktop.getHeight() ) y = 0; } catch( PropertyVetoException e ) {} } } } public void createInternalFrame( Component c, String s ) { JInternalFrame frame = new JInternalFrame( s, true, true, true, true ); // Add frame to the desktop frame.getContentPane().add( c ); desktop.add( frame, DOCLAYER ); frame.addVetoableChangeListener(this); int width = desktop.getWidth() / 2; int height = desktop.getHeight() / 2; frame.setBounds( 10, 10, 560, 600); //frame.reshape( nextFrameX, nextFrameY, width, height ); frame.setOpaque( true ); frame.setVisible(true); try { frame.setSelected( true ); } catch( PropertyVetoException pve ) {} // Compute distance between cascaded frames if ( frameDistance == 0 ) frameDistance = frame.getHeight() / 5; nextFrameX += frameDistance; nextFrameY += frameDistance; if ( nextFrameX + width > desktop.getWidth() ) nextFrameX = 0; if ( nextFrameY + height > desktop.getHeight() ) nextFrameY = 0; } public Component createEditorPane( URL u ) { JEditorPane panel = new JEditorPane(); panel.setEditable( false ); try { panel.setPage( u ); } catch ( IOException io ) { panel.setText("Error: " + io ); } return new JScrollPane( panel ); } public void vetoableChange( PropertyChangeEvent event ) throws PropertyVetoException { JInternalFrame frame = (JInternalFrame)event.getSource(); String name = event.getPropertyName(); Object value = event.getNewValue(); if ( name.equals ( "closed" ) && value.equals ( Boolean.TRUE ) ) { int result = JOptionPane.showInternalConfirmDialog ( frame, "Do you really want to close?" ); if ( result == JOptionPane.NO_OPTION || result == JOptionPane.CANCEL_OPTION ) { throw new PropertyVetoException ( "Closed canceled", event ); } } } public void actionPerformed( ActionEvent evt ) { Object source = evt.getSource(); if ( source == openItem ) { fc.addChoosableFileFilter( filter ); int returnVal = fc.showOpenDialog( EAT_Desktop.this ); if ( returnVal == JFileChooser.APPROVE_OPTION ) { File file = fc.getSelectedFile(); try { URL fileName = new URL( "file:///" + file.toString() ); createInternalFrame( createEditorPane( fileName ), fileName.toString() ); } catch (MalformedURLException m) { System.err.println("Malformed URL: " + file); } } } else if ( source == previewFile ) { fc.addChoosableFileFilter( wor_filter ); int returnVal = fc.showOpenDialog( EAT_Desktop.this ); if ( returnVal == JFileChooser.APPROVE_OPTION ) { File file = fc.getSelectedFile(); String if_wor = file.toString(); JInternalFrame doc = new WOR_FormFrame34( if_wor, savestg ); desktop.add(doc, DOCLAYER); try { doc.setVisible(true); doc.setSelected(true); } catch (java.beans.PropertyVetoException e2) {} } } else if ( source == previewRevFile ) { String savestg = "not-save"; fcr.addChoosableFileFilter( rev_filter ); int returnVal = fcr.showOpenDialog( EAT_Desktop.this ); if ( returnVal == JFileChooser.APPROVE_OPTION ) { File file = fcr.getSelectedFile(); String if_wor = file.toString(); JInternalFrame doc = new WOR_FormFrame34( if_wor, savestg ); desktop.add(doc, DOCLAYER); try { doc.setVisible(true); doc.setSelected(true); } catch (java.beans.PropertyVetoException e2) {} } } else if ( source == previewRevFile2 ) { ExampleFileFilter revfilter = new ExampleFileFilter(); for ( int x = 1; x < 100; x++ ) { String xnum = Integer.toString(x); revfilter.addExtension(xnum); } revfilter.setDescription("WOR History Files"); String savestg = "not-save"; fcr.addChoosableFileFilter( revfilter ); int returnVal = fcr.showOpenDialog( EAT_Desktop.this ); if ( returnVal == JFileChooser.APPROVE_OPTION ) { File file = fcr.getSelectedFile(); String if_wor = file.toString(); JInternalFrame doc = new WOR_FormFrame34( if_wor, savestg ); desktop.add(doc, DOCLAYER); try { doc.setVisible(true); doc.setSelected(true); } catch (java.beans.PropertyVetoException e2) {} } } else if ( source == newform ) { String if_wor = "null-stg"; JInternalFrame doc = new WOR_FormFrame34( if_wor, savestg ); desktop.add(doc, DOCLAYER); try { doc.setVisible(true); doc.setSelected(true); } catch (java.beans.PropertyVetoException e2) {} } else if ( source == runPing ) { String if_wor = "null-stg"; //new trw_Ping(); } else if ( source == openCal ) { JInternalFrame doc = new JCalendarFrame(); desktop.add(doc, DOCLAYER); try { doc.setVisible(true); doc.setSelected(true); } catch (java.beans.PropertyVetoException e2) {} } else if ( source == openPP ) { JInternalFrame help = new ProjectsPage(); desktop.add(help, DOCLAYER); try { help.setVisible(true); help.setSelected(true); } catch (java.beans.PropertyVetoException e2) {} } else if ( source == about ) { JOptionPane.showMessageDialog(this, ABOUTMSG); } else if ( source == exitItem ) { System.exit( 0 ); } else if ( source == nextItem ) { selectNextWindow(); } else if ( source == tileItem ) { tileWindows(); } else if ( source == cascadeItem ) { cascadeWindows(); } else if ( source == dragItem ) { desktop.putClientProperty( "JDesktopPane.dragMode", dragItem.isSelected() ? "outline" : null ); } } public static void main( String args[] ) { EAT_Desktop app = new EAT_Desktop(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); } }