Search in sources :

Example 16 with ComponentListener

use of java.awt.event.ComponentListener in project Universal-G-Code-Sender by winder.

the class MainWindow method main.

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    // Fix look and feel to use CMD+C/X/V/A instead of CTRL
    if (SystemUtils.IS_OS_MAC) {
        Collection<InputMap> ims = new ArrayList<>();
        ims.add((InputMap) UIManager.get("TextField.focusInputMap"));
        ims.add((InputMap) UIManager.get("TextArea.focusInputMap"));
        ims.add((InputMap) UIManager.get("EditorPane.focusInputMap"));
        ims.add((InputMap) UIManager.get("FormattedTextField.focusInputMap"));
        ims.add((InputMap) UIManager.get("PasswordField.focusInputMap"));
        ims.add((InputMap) UIManager.get("TextPane.focusInputMap"));
        int c = KeyEvent.VK_C;
        int v = KeyEvent.VK_V;
        int x = KeyEvent.VK_X;
        int a = KeyEvent.VK_A;
        int meta = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        for (InputMap im : ims) {
            im.put(KeyStroke.getKeyStroke(c, meta), DefaultEditorKit.copyAction);
            im.put(KeyStroke.getKeyStroke(v, meta), DefaultEditorKit.pasteAction);
            im.put(KeyStroke.getKeyStroke(x, meta), DefaultEditorKit.cutAction);
            im.put(KeyStroke.getKeyStroke(a, meta), DefaultEditorKit.selectAllAction);
        }
    }
    /* Create the form */
    GUIBackend backend = new GUIBackend();
    final MainWindow mw = new MainWindow(backend);
    /* Apply the settings to the MainWindow bofore showing it */
    boolean unitsAreMM = mw.settings.getDefaultUnits().equals(Units.MM.abbreviation);
    mw.fileChooser = new JFileChooser(mw.settings.getLastOpenedFilename());
    mw.commPortComboBox.setSelectedItem(mw.settings.getPort());
    mw.baudrateSelectionComboBox.setSelectedItem(mw.settings.getPortRate());
    mw.scrollWindowCheckBox.setSelected(mw.settings.isScrollWindowEnabled());
    mw.showVerboseOutputCheckBox.setSelected(mw.settings.isVerboseOutputEnabled());
    mw.showCommandTableCheckBox.setSelected(mw.settings.isCommandTableEnabled());
    mw.showCommandTableCheckBoxActionPerformed(null);
    mw.firmwareComboBox.setSelectedItem(mw.settings.getFirmwareVersion());
    mw.setSize(mw.settings.getMainWindowSettings().width, mw.settings.getMainWindowSettings().height);
    mw.setLocation(mw.settings.getMainWindowSettings().xLocation, mw.settings.getMainWindowSettings().yLocation);
    mw.addComponentListener(new ComponentListener() {

        @Override
        public void componentResized(ComponentEvent ce) {
            mw.settings.getMainWindowSettings().height = ce.getComponent().getSize().height;
            mw.settings.getMainWindowSettings().width = ce.getComponent().getSize().width;
        }

        @Override
        public void componentMoved(ComponentEvent ce) {
            mw.settings.getMainWindowSettings().xLocation = ce.getComponent().getLocation().x;
            mw.settings.getMainWindowSettings().yLocation = ce.getComponent().getLocation().y;
        }

        @Override
        public void componentShown(ComponentEvent ce) {
        }

        @Override
        public void componentHidden(ComponentEvent ce) {
        }
    });
    /* Display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            mw.setVisible(true);
        }
    });
    mw.initFileChooser();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            if (mw.fileChooser.getSelectedFile() != null) {
                mw.settings.setLastOpenedFilename(mw.fileChooser.getSelectedFile().getAbsolutePath());
            }
            mw.settings.setPort(mw.commPortComboBox.getSelectedItem().toString());
            mw.settings.setPortRate(mw.baudrateSelectionComboBox.getSelectedItem().toString());
            mw.settings.setScrollWindowEnabled(mw.scrollWindowCheckBox.isSelected());
            mw.settings.setVerboseOutputEnabled(mw.showVerboseOutputCheckBox.isSelected());
            mw.settings.setCommandTableEnabled(mw.showCommandTableCheckBox.isSelected());
            mw.settings.setFirmwareVersion(mw.firmwareComboBox.getSelectedItem().toString());
            SettingsFactory.saveSettings(mw.settings);
            if (mw.pendantUI != null) {
                mw.pendantUI.stop();
            }
        }
    });
    // Check command line for a file to open.
    boolean open = false;
    for (String arg : args) {
        if (open) {
            try {
                backend.setGcodeFile(new File(arg));
                open = false;
            } catch (Exception ex) {
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                System.exit(1);
            }
        }
        if (arg.equals("--open") || arg.equals("-o")) {
            open = true;
        }
    }
}
Also used : ComponentListener(java.awt.event.ComponentListener) GUIBackend(com.willwinder.universalgcodesender.model.GUIBackend) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) javax.swing(javax.swing) ComponentEvent(java.awt.event.ComponentEvent) File(java.io.File)

Example 17 with ComponentListener

use of java.awt.event.ComponentListener in project OpenNotebook by jaltekruse.

the class OpenNotebook method createAndShowGUI.

private static void createAndShowGUI(boolean askMode, boolean blankDoc, boolean studentMode) {
    frame = new JFrame("OpenNotebook - Teacher");
    application = new OpenNotebook(askMode, blankDoc, studentMode);
    if (frame == null) {
        // OpenNotebook constructor
        return;
    }
    frame.getContentPane().add(application);
    Dimension frameDim = new Dimension(1200, 720);
    frame.setPreferredSize(frameDim);
    frame.addComponentListener(new ComponentListener() {

        @Override
        public void componentHidden(ComponentEvent arg0) {
        }

        @Override
        public void componentMoved(ComponentEvent arg0) {
        }

        @Override
        public void componentResized(ComponentEvent arg0) {
        // System.out.println("resized!!!");
        // if (frame.getWidth() > 1100) {
        // application.setDocAlignment(ALIGN_DOCS_CENTER);
        // }
        // if (frame.getWidth() <= 1100) {
        // application.setDocAlignment(ALIGN_DOCS_RIGHT);
        // }
        }

        @Override
        public void componentShown(ComponentEvent arg0) {
        }
    });
    application.addComponentListener(new ComponentListener() {

        @Override
        public void componentHidden(ComponentEvent arg0) {
        }

        @Override
        public void componentMoved(ComponentEvent arg0) {
        }

        @Override
        public void componentResized(ComponentEvent arg0) {
        // System.out.println("resized!!!");
        // if (application.getWidth() > 1100) {
        // application.setDocAlignment(ALIGN_DOCS_CENTER);
        // }
        // if (application.getWidth() <= 1100) {
        // application.setDocAlignment(ALIGN_DOCS_RIGHT);
        // }
        }

        @Override
        public void componentShown(ComponentEvent arg0) {
        }
    });
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(createWindowListener());
    frame.pack();
    frame.setVisible(true);
}
Also used : ComponentListener(java.awt.event.ComponentListener) JFrame(javax.swing.JFrame) Dimension(java.awt.Dimension) ComponentEvent(java.awt.event.ComponentEvent)

Example 18 with ComponentListener

use of java.awt.event.ComponentListener in project druid by alibaba.

the class DruidPanel method addOrRefreshTable.

/**
 * 如果是第一次调用,则生成表格对象;否则根据当前时间来和上次刷新时间的间隔,
 * 是否大于对象初始化时设定的时间间隔来判断是否刷新表格数据。
 *
 * @param url service的地址
 * @param conn MBeanServerConnection对象
 * @throws Exception
 */
protected void addOrRefreshTable(String url) throws Exception {
    if (url != null) {
        boolean needRefresh = false;
        long timeNow = new Date().getTime();
        if (scrollPane == null) {
            table = new JTable();
            scrollPane = new JScrollPane();
            scrollPane.setAutoscrolls(true);
            scrollPane.setBorder((TitledBorder) BorderFactory.createTitledBorder("数据区"));
            setLayout(null);
            scrollPane.setBounds(10, 10, getWidth() - 20, getHeight() - 80);
            this.add(scrollPane);
            copyrightPanel = new JPanel();
            copyrightPanel.setBorder((TitledBorder) BorderFactory.createTitledBorder("版权区"));
            JLabel authorInfo = new JLabel(COPYRIGHT_STRING);
            copyrightPanel.add(authorInfo);
            this.add(copyrightPanel);
            copyrightPanel.setBounds(10, getHeight() - 60, getWidth() - 20, 60);
            needRefresh = true;
            lastRefreshTime = timeNow;
            this.addComponentListener(new ComponentListener() {

                @Override
                public void componentShown(ComponentEvent arg0) {
                }

                @Override
                public void componentResized(ComponentEvent arg0) {
                    scrollPane.setBounds(10, 10, getWidth() - 20, getHeight() - 80);
                    copyrightPanel.setBounds(10, getHeight() - 60, getWidth() - 20, 60);
                }

                @Override
                public void componentMoved(ComponentEvent arg0) {
                }

                @Override
                public void componentHidden(ComponentEvent arg0) {
                }
            });
        } else {
            if (lastRefreshTime + activeTime < timeNow) {
                needRefresh = true;
                lastRefreshTime = timeNow;
            }
        }
        if (needRefresh) {
            LOG.debug("refresh" + timeNow);
            ArrayList<LinkedHashMap<String, Object>> data = TableDataProcessor.parseData(TableDataProcessor.getData(url, conn));
            if (data != null) {
                tableDataProcess(data);
            }
        }
    } else {
        // url不存在
        LOG.warn("url不存在");
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) ComponentListener(java.awt.event.ComponentListener) JTable(javax.swing.JTable) JLabel(javax.swing.JLabel) ComponentEvent(java.awt.event.ComponentEvent) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ComponentListener (java.awt.event.ComponentListener)18 ComponentEvent (java.awt.event.ComponentEvent)14 Component (java.awt.Component)4 Dimension (java.awt.Dimension)4 JScrollPane (javax.swing.JScrollPane)4 Point (java.awt.Point)3 JLabel (javax.swing.JLabel)3 JTextArea (javax.swing.JTextArea)3 ComponentAdapter (java.awt.event.ComponentAdapter)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 javax.swing (javax.swing)2 DefaultListCellRenderer (javax.swing.DefaultListCellRenderer)2 Helper (chatty.Helper)1 Action (chatty.gui.components.JListActionHelper.Action)1 ContextMenuListener (chatty.gui.components.menus.ContextMenuListener)1 StreamInfosContextMenu (chatty.gui.components.menus.StreamInfosContextMenu)1