📄 appmanagerpanel.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.gui;import java.awt.*;import java.awt.event.*;import java.util.Vector;import javax.swing.*;import javax.swing.border.BevelBorder;/** * * @author randy */public class AppManagerPanel extends JPanel implements ItemListener, ActionListener { private JDialog frame; private Vector<String> originalAutoRunClasses; private Vector<String> autoRunClasses; private Vector<String> debugClasses; private Vector<String> runClasses; private Vector<String> allClasses; private Vector<JCheckBox> autoRunCheckBoxes; private Vector<JCheckBox> debugCheckBoxes; private Vector<JCheckBox> runCheckBoxes; private boolean userHasDoneSomething; private boolean userHitOkOrCancel; private JButton okButton; private JButton cancelButton; private boolean effectChange; // Take action on user changes or not? (User may cancel for "No.") public AppManagerPanel(){ } public void init(){ originalAutoRunClasses = new Vector<String>(); autoRunClasses = new Vector<String>(); debugClasses = new Vector<String>(); runClasses = new Vector<String>(); autoRunCheckBoxes = new Vector<JCheckBox>(); debugCheckBoxes = new Vector<JCheckBox>(); runCheckBoxes = new Vector<JCheckBox>(); userHasDoneSomething = false; userHitOkOrCancel = false; effectChange = false; } public static AppManagerPanel openNew(JFrame parentFrame, Vector<String> classNames, Vector<String> initialAutoRunClassNames){ AppManagerPanel instance = new AppManagerPanel(); instance.init(); instance.setAllClasses(classNames); instance.setOriginalAutoRunClasses(initialAutoRunClassNames); instance.setLayout(new BorderLayout()); instance.createCheckBoxes(); instance.addComponents(); instance.setOpaque(true); //content panes must be opaque instance.createFrame(parentFrame); //Create and set up the window. return instance; } public void createFrame(JFrame parentFrame){ frame = new JDialog(parentFrame, "Application Manager", true){ //true indicates it IS modal public void dispose(){ if( userHasDoneSomething && ! userHitOkOrCancel){ effectChange = askUserApplyChanges(); } super.dispose(); } }; frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setContentPane(this); //Display the window. frame.pack(); frame.setLocationRelativeTo(parentFrame); frame.setVisible(true); } public void createCheckBoxes(){ for (String cn : allClasses){ boolean foundIt = false; for(String arcn : originalAutoRunClasses){ if(cn.equals(arcn)) foundIt = true; } JCheckBox arcb = new JCheckBox(); if(foundIt) arcb.setSelected(true); arcb.addItemListener(this); autoRunCheckBoxes.add(arcb); JCheckBox cb = new JCheckBox(); cb.addItemListener(this); debugCheckBoxes.add(cb); cb = new JCheckBox(); cb.addItemListener(this); runCheckBoxes.add(cb); } } public void addComponents(){ JPanel gbPanel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.weightx = 0.5; c.weighty = 0.5; JLabel autoRunColHead = new JLabel("Auto run on reboot"); JLabel appNameColHead = new JLabel("Application"); JLabel debugColHead = new JLabel("Debug now"); JLabel runColHead = new JLabel("Run now"); c.gridy = 0; c.gridx = 0; c.insets = new Insets(0,3,0,3); gbPanel.add(autoRunColHead, c); c.gridx = 1; gbPanel.add(appNameColHead,c);// c.gridx = 2;// gbPanel.add(debugColHead,c);// c.gridx = 3;// gbPanel.add(runColHead, c); c.gridy = 1; c.gridx = 0; c.gridwidth = 2; JPanel line = new JPanel(); line.setSize(200, 3); line.setForeground(Color.WHITE); line.setBackground(Color.WHITE); gbPanel.add(line, c); c.gridy = 2; c.gridwidth = 1; for (int i = 0; i < allClasses.size(); i++) { c.gridx = 0; gbPanel.add(autoRunCheckBoxes.elementAt(i), c); c.gridx = 1; String fullName = allClasses.elementAt(i); String[] tokens = fullName.split("\\."); String className = tokens[tokens.length - 1]; gbPanel.add(new JLabel(className), c);// c.gridx = 2;// gbPanel.add(debugCheckBoxes.elementAt(i), c);// c.gridx = 3;// gbPanel.add(runCheckBoxes.elementAt(i), c); c.gridy = c.gridy + 1; } add(gbPanel, BorderLayout.CENTER); JPanel okCancelPanel = new JPanel(); okCancelPanel.setLayout(new BoxLayout(okCancelPanel, BoxLayout.X_AXIS)); okButton = new JButton("OK"); okButton.addActionListener(this); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(this); okCancelPanel.add(Box.createHorizontalGlue()); okCancelPanel.add(okButton); okCancelPanel.add(cancelButton); okButton.setMnemonic(KeyEvent.VK_ENTER); okCancelPanel.setBorder(BorderFactory.createEmptyBorder( 10, 0, 0, 0)); add(okCancelPanel, BorderLayout.PAGE_END); setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); okButton.setSelected(true); okButton.setFocusPainted(true); } /** Listens to the check boxes. */ public void itemStateChanged(ItemEvent e) { userHasDoneSomething = true; int index = 0; char c = '-'; Object source = e.getItemSelectable(); for(int i = 0; i < autoRunCheckBoxes.size(); i++){ if(source == autoRunCheckBoxes.elementAt(i)){ // This i is the chosen checkBox. Now: was it selected or unselected if(e.getStateChange() == ItemEvent.SELECTED){ autoRunClasses.add(allClasses.elementAt(i)); } if(e.getStateChange() == ItemEvent.DESELECTED){ if(autoRunClasses.contains(allClasses.elementAt(i))){ autoRunClasses.remove(allClasses.elementAt(i)); } } } } } public void actionPerformed(ActionEvent e){ userHitOkOrCancel = true; if(e.getSource() == okButton) { effectChange = true; } if(e.getSource() == cancelButton){ effectChange = false; } frame.dispose(); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("CheckBoxDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new AppManagerPanel(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } public boolean askUserApplyChanges(){ Object[] options = { "No", "Yes"}; int n = JOptionPane.showOptionDialog(null, "Apply these changes?", "", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); switch(n){ case(0): return false; case(1): return true; } return false; } public Vector getAutoRunClasses() { if(effectChange) return autoRunClasses; else return originalAutoRunClasses; } public Vector getDebugClasses() { return debugClasses; } public Vector getRunClasses() { return runClasses; } public void setAllClasses(Vector allClasses) { this.allClasses = allClasses; } public void setOriginalAutoRunClasses(Vector<String> arcs) { originalAutoRunClasses = arcs; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -