⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treatmentwizard1.java

📁 eclipse平台的CDT项目3.0版本的源代码
💻 JAVA
字号:
/* * Oct 25, 2002 * * Document me! */package cdt.projects.tree.wizards.treatment;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import javax.swing.*;import javax.swing.border.Border;import javax.swing.border.TitledBorder;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import cdt.gui.HTMLTextPane;import cdt.projects.Project;import cdt.projects.tree.nodes.*;import cdt.wizard.WizardFrame;import cdt.Frame1;public class TreatmentWizard1 extends WizardFrame implements ActionListener {    /** The parent frame to this wizard. */    private JFrame parent;    /** The JList containing the treatments. */    private JList treatmentList;    /** Hashtable where the key is the diffDiag node, and the value is the finalDiag node. */    private ArrayList treatments;    /** JPanel containing the treatment information. */    private JPanel treatmentPanel;    /**     * The {@link cdt.gui.HTMLTextPane HTMLTextPane} containing     * the text of the differential diagnosis feedback.     */    private HTMLTextPane treatmentText;    /** The treatment folder in the project. */    private final Hold oldTreatmentFolder;    /**     * Creates the differential diagnosis wizard.     *     * @param parent The parent JFrame, in this case typically an instance of     *      {@link cdt.Frame1 Frame1}.     */    public TreatmentWizard1(JFrame parent) {        super(parent);        this.parent = parent;        this.treatments = new ArrayList();        this.oldTreatmentFolder = (Hold)this.data.get("oldTreatmentFolder");        Enumeration enum = oldTreatmentFolder.children();        while(enum.hasMoreElements()) {            Node treatmentNode = (Node)enum.nextElement();            if(treatmentNode instanceof Item) {                this.treatments.add(treatmentNode.copy());            }        }        init();    }    /**     * Sets up the layout of this page.     */    public void customize() {        showPreviousButton(true);        showFinishButton(true);        showNextButton(false);        setTitle("Treatment Wizard");        Dimension dim;        // Sets up a JList of the treatments        Iterator iter = this.treatments.iterator();        DefaultListModel listModel = new DefaultListModel();        while(iter.hasNext()) {            String diagnosisName = ((Node)iter.next()).getName();            listModel.addElement(diagnosisName);        }        this.treatmentList = new JList(listModel);        this.treatmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        this.treatmentList.addListSelectionListener(new TreatmentWizard1.TreatmentSelectionListener());        // Puts the list into a JScrollPane        dim = new Dimension(120, 150);        JScrollPane scrollPane = new JScrollPane(this.treatmentList);        scrollPane.setMaximumSize(dim);        scrollPane.setPreferredSize(dim);        scrollPane.setMinimumSize(dim);        // Puts the diagnosis list's scrollpane in a JPanel with the [ + ] and [ - ] buttons        JPanel leftPanel = new JPanel();        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));        leftPanel.add(scrollPane);        leftPanel.add(Box.createVerticalStrut(5));        JButton plus = new JButton("Add Treatment");        plus.addActionListener(this);        dim = new Dimension(135, 30);        plus.setMaximumSize(dim);        plus.setMinimumSize(dim);        plus.setPreferredSize(dim);        JPanel temp = new JPanel();        temp.add(plus);        leftPanel.add(temp);        JButton minus = new JButton("Remove Treatment");        minus.addActionListener(this);        temp = new JPanel();        dim = new Dimension(135, 30);        minus.setMaximumSize(dim);        minus.setMinimumSize(dim);        minus.setPreferredSize(dim);        temp.add(minus);        leftPanel.add(temp);        // Creates the HTMLTextPane for the Differential Diagnosis text        this.treatmentText = new HTMLTextPane(this.parent, "");        this.treatmentText.setEditable(false);        this.treatmentText.setBackground(Color.lightGray);        dim = new Dimension(360, 120);        JScrollPane diffDiagScrollPane = new JScrollPane(this.treatmentText);        diffDiagScrollPane.setPreferredSize(dim);        diffDiagScrollPane.setMinimumSize(dim);        diffDiagScrollPane.setMaximumSize(dim);        Border b = BorderFactory.createLineBorder(Color.darkGray);        b = BorderFactory.createTitledBorder(b, "Treatment Text");        ((TitledBorder)b).setTitleJustification(TitledBorder.CENTER);        diffDiagScrollPane.setBorder(b);        // Creates the panel containing the check box, and the two HTMLTextPanes        this.treatmentPanel = new JPanel();        this.treatmentPanel.setLayout(new BoxLayout(this.treatmentPanel, BoxLayout.Y_AXIS));        this.treatmentPanel.add(diffDiagScrollPane);        setupBorder(this.treatmentPanel, "No Treatment Selected");        // Organizes the main panel        JPanel mainPanel = new JPanel();        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));        mainPanel.add(leftPanel);        mainPanel.add(Box.createHorizontalStrut(5));        mainPanel.add(new JSeparator(JSeparator.VERTICAL));        mainPanel.add(Box.createHorizontalStrut(5));        mainPanel.add(this.treatmentPanel);        centerPanel.add(mainPanel);        pack();    }    /**     * Sets up an HTMLEditorKit with the appropriate border.     *     * @param c The component to put the border onto.     * @param title The title for the border.     */    private void setupBorder(JComponent c, String title) {        TitledBorder b = BorderFactory.createTitledBorder(title);        b.setTitleJustification(TitledBorder.CENTER);        c.setBorder(b);    }    /**     * Moves to the next page in the wizard.     */    public void nextAction() {    }    /**     * A button has been pressed.     */    public void actionPerformed(ActionEvent e) {        // Adds a diagnosis        if(e.getActionCommand().equals("Add Treatment")) {            String name = JOptionPane.showInputDialog(this, "Please enter name of a new treatment.");            if(null != name && false == name.equals("")) {                // Makes sure this name is not a duplicate                Iterator iter = treatments.iterator();                while(iter.hasNext()) {                    Node n = (Node)iter.next();                    if(n.getName().equals(name)) {                        cdt.ErrorD.ErrorDlg.ErrorMsg("There is already a treatment with this name.");                        return;                    }                }                // Disables any ChoiceSelectionListeners on the diagnosisList to prevent                // unwanted behavior                ListSelectionListener listListeners[] = treatmentList.getListSelectionListeners();                for(int i = 0; i < listListeners.length; ++i) {                    if(listListeners[i] instanceof TreatmentWizard1.TreatmentSelectionListener) {                        ((TreatmentWizard1.TreatmentSelectionListener)listListeners[i]).setActive(false);                    }                }                DefaultListModel listModel = (DefaultListModel)treatmentList.getModel();                listModel.addElement(name);                treatmentList.setModel(listModel);                // Adds a new diagnosis Item nodes to the treatments HashMap                treatments.add(new Item(name));                // Re-enables any ChoiceSelectionListeners that were disabled during the                // execution of this method.                for(int i = 0; i < listListeners.length; ++i) {                    if(listListeners[i] instanceof TreatmentWizard1.TreatmentSelectionListener) {                        ((TreatmentWizard1.TreatmentSelectionListener)listListeners[i]).setActive(true);                    }                }                treatmentList.setSelectedIndex(listModel.getSize()-1);            }        }        // Removes a diagnosis        else if(e.getActionCommand().equals("Remove Treatment")) {            final int selectedIndex = this.treatmentList.getSelectedIndex();            DefaultListModel listModel = new DefaultListModel();            ListModel oldList = treatmentList.getModel();            for(int i = 0; i < oldList.getSize(); ++i) {                if(i != selectedIndex) {                    listModel.addElement(oldList.getElementAt(i));                } else {                    // Removes the treatment from the ArrayList                    Iterator iter = treatments.iterator();                    while(iter.hasNext()) {                        Node n = (Node)iter.next();                        if(n.getName().equals(oldList.getElementAt(i))) {                            iter.remove();                            break;                        }                    }                }            }            treatmentList.setModel(listModel);            if(selectedIndex < listModel.size()) {                treatmentList.setSelectedIndex(selectedIndex);            } else {                treatmentText.setText("");                treatmentText.setEditable(false);                treatmentText.setBackground(Color.lightGray);                setupBorder(treatmentPanel, "No Treatment Selected");            }        }    }    /**     * Saves whatever data is in the currently open {@link cdt.gui.HTMLTextPane HTMLTextPane's}.     */    private final void saveTreatment() {        final int selectedChoice = this.treatmentList.getSelectedIndex();        if(-1 != selectedChoice) {            final String selectedTreatment = (String)treatmentList.getModel().getElementAt(selectedChoice);            // Saves the text for the treatment.            Iterator iter = treatments.iterator();            while(iter.hasNext()) {                Node n = (Node)iter.next();                if(n.getName().equals(selectedTreatment)) {                    n.setData(treatmentText.getText());                    break;                }            }        }    }    /**     * Creates the treatments and closes the wizard.     */    public void finishAction() {        saveTreatment();        if(null != oldTreatmentFolder) {            // Clears out the old treatment folder in preparation of inserting the treatments from the wizard            Enumeration enum = oldTreatmentFolder.children();            while(enum.hasMoreElements()) {                Node n = (Node)enum.nextElement();                if(n instanceof Item) {                    n.removeNode();                }            }            // Adds the new treatments into the treatments folder            Iterator iter = treatments.iterator();            while(iter.hasNext()) {                Node n = (Node)iter.next();                n.setFile(Node.foldername(n.getName()) + ".html");                oldTreatmentFolder.addNodeInto(n);            }            // Changes the wizard's name if necessary            Node n = Node.findChildNode(oldTreatmentFolder, "Create treatments");            if(null != n) {                n.setName("Modify treatments");            }        }        Project.currentProject.getTree().updateUI();        // Cleans up everything and closes the wizard        super.finishAction();    }    /**     * Changes the text in the editor according to what choice is selected.     */    private class TreatmentSelectionListener implements ListSelectionListener {        /** The currently 'selected' choice's index. */        private int currentSelection;        /** Whether this listener is currently listening. */        private boolean isActive;        /**         * Creates a ChoiceSelectionListener that will affect a JTextComponent.         */        public TreatmentSelectionListener() {            this.isActive = true;            this.currentSelection = -1;        }        public boolean isActive() {            return this.isActive;        }        public void setActive(boolean isActive) {            this.isActive = isActive;        }        /**         * Fired when an item in the registered list is selected.         *         * @param e The ListSelectionEvent that describes the selection.         */        public synchronized void valueChanged(ListSelectionEvent e) {            if(false == isActive() || e.getValueIsAdjusting()) { return; }            final JList treatmentList = (JList)e.getSource();            final int newIndex = treatmentList.getSelectedIndex();            if(newIndex == this.currentSelection ) {                // The selection hasn't changed, so don't do anything                return;            } else if(newIndex == -1) {                this.currentSelection = -1;                return;            }            final String nextSelectedTreatment = (String)treatmentList.getModel().getElementAt(newIndex);            // Saves the text from the previously selected answer            if(-1 != this.currentSelection) {                String selectedTreatment = (String)treatmentList.getModel().getElementAt(this.currentSelection);                // Saves the text for the differential diagnosis.                Iterator iter = treatments.iterator();                while(iter.hasNext()) {                    Node n = (Node)iter.next();                    if(n.getName().equals(selectedTreatment)) {                        n.setData(treatmentText.getText());                        break;                    }                }            }            // Makes sure that the answerText and the feedbackText are            // editable when an answer is selected from the list            if(false == treatmentText.isEditable()) {                treatmentText.setEditable(true);                treatmentText.setBackground(UIManager.getColor("controlLtHighlight"));            }            // Switches the text in the answerText and feedbackText to the            // text corresponding to the answer that was selected            Iterator iter = treatments.iterator();            while(iter.hasNext()) {                Node n = (Node)iter.next();                if(n.getName().equals(nextSelectedTreatment)) {                    treatmentText.setText(n.getData());                    break;                }            }            setupBorder(treatmentPanel, nextSelectedTreatment);            this.currentSelection = newIndex;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -