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

📄 optionsdialog.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
* Copyright (c) 2006, University of Kent
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this 
* list of conditions and the following disclaimer.
* 
* Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation 
* and/or other materials provided with the distribution. 
*
* 1. Neither the name of the University of Kent nor the names of its 
* contributors may be used to endorse or promote products derived from this 
* software without specific prior written permission. 
*
* 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS  
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
* PURPOSE ARE DISCLAIMED. 
*
* 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES.  IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/

/*
 * OptionsDialog.java - 25/01/2006
 */

package issrg.editor2.configurations;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * The Options/Configuration Dialog is the dialog that displays
 * when the user clicks on the Tools/Configure Menu. The Dialog
 * adds a JTabbedPane to the display and adds the required tabs 
 * that contain the panels with the settings that the user can 
 * modify.
 * <p>
 * So far the Dialog Only contains one tab. This is for the LDAP 
 * Configurations. Although other tabs could be added in a similar
 * manner.
 * <p>
 * The LDAP Configuration, consists in that the user could add,
 * delete or modify any new or existing LDAP directories. 
 * The dialog expects a change in the text fields or a selection
 * from the available connections, in order for the function 
 * buttons to be made available. This is to guide the user.
 * Directory Name, DNS Name and Port number, are compulsory for 
 * each Directory.
 * <p>
 * The Options Dialog contains two buttons. Save Changes, and 
 * Close. When Save Changes is pressed, it will save the modifications
 * and exit the dialog, whilst hitting close will forget the 
 * modifications and exit the dialog.
 *
 * @author  Christian Azzopardi
 */

public class OptionsDialog extends JDialog implements ActionListener, WindowListener, ChangeListener
{
    /**
     * The Dialog Specific XML Editor. Not Permis Policy XML, but the valid XML 
     * for the Options Dialog.
     */
    ConfigurationComponent configComp;
    
    /**
     * The Buttons that are used for Saving and Exiting or 
     * just exiting. 
     */
    JButton saveButton, closeButton;  
    
    /**
     * The Tabbed Pane that is added to the main dialog 
     * display, and allows for new panels to be added to it. 
     */
    JTabbedPane tabbedPane;
    
    /**
     * Location of the ResourceBundle, and we then obtain the 
     * relevant (String) resources from the bundle. Basically
     * this loads a set of strings from the bundle. The exact
     * english content of the resource is commented next to the
     * String decleration, these will change with different 
     * language Resource Bundles.
     */
    ResourceBundle rbl = ResourceBundle.getBundle("issrg/editor2/PEComponent_i18n");
    String errorHeader = rbl.getString("ErrorHeader");            //contains Error!
    String infoHeader = rbl.getString("InfoHeader");              //contains Info!
    String saveButtonLabel = rbl.getString("LDAPDialogButton1");  //contains Save
    String closeButtonLabel = rbl.getString("LDAPDialogButton2"); //contains Close
    String ldapDirSetup = rbl.getString("LDAPDialogTitle");       //contains LDAP Directory Setup
    String envSetupCaption = rbl.getString("Environment_Parameters_ConfigDialog_Title"); //contains Environment Parameters
    String roleTypeSetupCaption = rbl.getString("RoleType_Parameters_ConfigDialog_Title"); //contains Role Types
    String appPreferencesCaption = rbl.getString("Application_Preferences_Main_Title"); //contains Application Preferences
    String tapConfigCaption = rbl.getString("TAP_File_Preferences_ConfigDialog_Title"); //contains TAP Files
    String wsdlConfigCaption = rbl.getString("WSDL_File_Preferences_ConfigDialog_Title"); //contains Protected Web Services
    String mainDialogTitle = rbl.getString("MainDialogTitle");    //contains Options
    
    /** 
     * Creates a new instance of OptionsDialog. This instance is created 
     * in the PEApplication Class. It is not set visible in this class. 
     * The scope is that the dialog will be 'always running' and will only 
     * be made visible when the shortcut to it from the main menu is 
     * clicked. It will retain the focus until closed. 
     * <p> 
     * The Constructor simply calls its superclass constructor setting 
     * owner which in this case is the PEApplication. 
     * <p>
     * A WindowListener is needed to notify when the window has been 
     * closed.
     * <p>
     * The Tabbed Pane is added to the dialog display, and the first
     * tab is added.
     *
     * @param that      the Dialog Specific XML Editor.
     * @param owner     the owner of the Dialog.
     */
    public OptionsDialog(ConfigurationComponent that, Frame owner)
    {
        super(owner, "", true);
        
        this.rootPane.registerKeyboardAction(this, "ESC_KEY", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
        
        this.setTitle(mainDialogTitle);
        this.configComp = that;
        this.addWindowListener(this);
        tabbedPane = new JTabbedPane();
        tabbedPane.addChangeListener(this);
        
        tabbedPane.add(ldapDirSetup, createPanel(this.configComp.ldapConfig));
        tabbedPane.add(envSetupCaption, createPanel(this.configComp.envParams));
        tabbedPane.add(roleTypeSetupCaption, createPanel(this.configComp.roleTypeParams));
        tabbedPane.add(appPreferencesCaption, createPanel(this.configComp.appPreferences));
        tabbedPane.add(tapConfigCaption, createPanel(this.configComp.tapFileConfig));
        tabbedPane.add(wsdlConfigCaption, createPanel(this.configComp.wsdlFileConfig));
        
        this.getContentPane().add(tabbedPane);
        this.pack();
        this.setResizable(false);        
    }
    
    public JPanel createPanel(JPanel whichPanel)
    {
        JPanel DialogPanel = new JPanel();
        JPanel Dir = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        Dir.add(whichPanel, c);

        saveButton = new JButton("Ok");
        saveButton.addActionListener(this);
        saveButton.setActionCommand("WriteXMLtoFile");
        closeButton = new JButton("Cancel");
        closeButton.addActionListener(this);
        closeButton.setActionCommand("Close");
        setCaption("CLOSE_BUTTON", closeButtonLabel);
        setCaption("SAVE_BUTTON", saveButtonLabel);
        
        JPanel buttons = new JPanel(new FlowLayout());
        buttons.add(saveButton);
        buttons.add(closeButton);

        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 0;
        c.weighty = 0;
        Dir.add(buttons, c);

        DialogPanel.add(Dir);
        
        return DialogPanel;
    }
    
    /**
     * When actionEvents are fired, the method will check from which
     * button the action has come. 
     * <p>
     * If the save button was clicked, the method will open the
     * existing Config File (pe.cfg) and will Generate the new XML
     * to save into that file. 
     * It will also unset the visibility of the Dialog, and create
     * new Stacks for the undo/redo.
     * (Otherwise when reopening the dialog box we can undo/redo 
     * the actions that were done before hitting this button)
     * <p>
     * If the Close button is hit, it will reset the undo/redo stacks
     * and will unset the visibility of the dialog.
     */
    public void actionPerformed(ActionEvent av)
    {
        if (av.getActionCommand().intern().equals("ESC_KEY")) {
            replaceItemsFromStack();
            this.setVisible(false);
        }
        if (av.getActionCommand().intern().equals("WriteXMLtoFile")) {
            writeToFile();      
        } else if (av.getActionCommand().intern().equals("Close")) {
            //this.configComp.removeXMLChangeListener(this.configComp.tapFileConfig);
            replaceItemsFromStack();
            //this.configComp.addXMLChangeListener(this.configComp.tapFileConfig);
            this.setVisible(false);
        }
    }
    
    public void writeToFile()
    {
        FileOutputStream file; 
        try
        {
            file = new FileOutputStream(configComp.getFileName());
            configComp.GenerateXML(file);
            file.close();
            this.setVisible(false);
            configComp.newDoneStack();
            configComp.newUnDoneStack();
        }
        catch(IOException e)
        {
        }
    }
    
    /**
     * This will modify the undo/redo stacks as to their previous state.
     */
    public void replaceItemsFromStack()
    {
        for (int i = 0; i < configComp.done.capacity(); i++)
        {
            configComp.undo();
        }        
    }

    public void windowOpened(WindowEvent e)
    {
    }
    
    public void windowIconified(WindowEvent e)
    {
    }
        
    public void windowDeiconified(WindowEvent e)
    {
    }
    
    public void windowDeactivated(WindowEvent e)
    {
    }
    
    /**
     * When the dialog is closed, the window will behave in the same 
     * manner as if a user clicked on the close button. 
     * <p>
     * It will reset the undo/redo stacks, and unset the visibility of 
     * the dialog.
     */
    public void windowClosing(WindowEvent e)
    {
        replaceItemsFromStack();
        this.setVisible(false);
    }
    
    public void windowClosed(WindowEvent e)
    {
    }
    
    public void windowActivated(WindowEvent e)
    {
    }       
    
   /**
    *  Sets the text the component will show. If component parameter is not found 
    *  in the list of parameters, the method will call its super method to attempt
    *  to find the component there.
    *
    *  @param internalName         The internal name used in the program.
    *  @param internationalName    The international name to set the text to.
    */       
    public void setCaption(String internalName, String internationalName)
    {
        if (internalName.equals("CLOSE_BUTTON"))
        {
            closeButton.setText(internationalName);
        }
        else if (internalName.equals("SAVE_BUTTON"))
        {
            saveButton.setText(internationalName);
        }
    }
    
    /**
     * Overridden setVisible method. Needed to refresh the dialog window
     * correctly each time it is opened.
     */
    public void setVisible(boolean b)
    {
        configComp.ldapConfig.listBox.clearSelection();
        configComp.ldapConfig.itemSelected();
        super.setVisible(b);
    }
    
    public void stateChanged(ChangeEvent e)
    {
        JTabbedPane tPane = (JTabbedPane)e.getSource();
        for (int i = 0; i < tPane.getTabCount(); i++)
        {
            tPane.setForegroundAt(i, Color.BLACK);
        }
        if (tPane.getSelectedIndex() > -1)
            tPane.setForegroundAt(tPane.getSelectedIndex(), Color.BLUE);
    }
}

⌨️ 快捷键说明

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