usr/src/java/vpanels/panel/org/opensolaris/os/vp/panel/swing/control/SettingsControl.java
changeset 547 e1d8b4ddb166
parent 493 b722c21cb2c3
child 658 278fba81c0c1
equal deleted inserted replaced
546:b284ba298d57 547:e1d8b4ddb166
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package org.opensolaris.os.vp.panel.swing.control;
       
    27 
       
    28 import java.awt.event.*;
       
    29 import java.net.URL;
       
    30 import java.util.List;
       
    31 import org.opensolaris.os.vp.panel.common.ClientContext;
       
    32 import org.opensolaris.os.vp.panel.common.action.*;
       
    33 import org.opensolaris.os.vp.panel.common.control.*;
       
    34 import org.opensolaris.os.vp.panel.common.model.PanelDescriptor;
       
    35 import org.opensolaris.os.vp.util.misc.ChangeableAggregator;
       
    36 import org.opensolaris.os.vp.util.swing.SettingsPanel;
       
    37 
       
    38 /**
       
    39  * {@code SettingsControl} extends class {@link SwingControl} to provide
       
    40  * support for {@link SettingsPanel}s.
       
    41  */
       
    42 public class SettingsControl<P extends PanelDescriptor,
       
    43     C extends SettingsPanel> extends SwingControl<P, C> {
       
    44 
       
    45     //
       
    46     // Constructors
       
    47     //
       
    48 
       
    49     public SettingsControl(String id, String name, ClientContext context) {
       
    50 	super(id, name, context);
       
    51     }
       
    52 
       
    53     public SettingsControl(String id, String name, P descriptor) {
       
    54 	super(id, name, descriptor);
       
    55     }
       
    56 
       
    57     //
       
    58     // SwingControl methods
       
    59     //
       
    60 
       
    61     /**
       
    62      * Returns {@code getComponent().getChangeableAggregator()}.
       
    63      */
       
    64     @Override
       
    65     public ChangeableAggregator getChangeableAggregator() {
       
    66 	C comp = getComponent();
       
    67 	if (comp != null) {
       
    68 	    return comp.getChangeableAggregator();
       
    69 	}
       
    70 	return null;
       
    71     }
       
    72 
       
    73     //
       
    74     // SettingsControl methods
       
    75     //
       
    76 
       
    77     /**
       
    78      * Adds a listener to the <strong>Apply</strong> button of this {@code
       
    79      * SettingsControl}'s {@link SettingsPanel} to invoke this {@link
       
    80      * Control}'s save action.
       
    81      */
       
    82     protected void addDefaultApplyAction() {
       
    83 	getComponent().getButtonBar().getApplyButton().addActionListener(
       
    84 	    new ActionListener() {
       
    85 		@Override
       
    86 		public void actionPerformed(ActionEvent e) {
       
    87 		    getSaveAction().asyncInvoke();
       
    88 		}
       
    89 	    });
       
    90     }
       
    91 
       
    92     /**
       
    93      * Adds a listener to the <strong>Back</strong> button of this {@code
       
    94      * SettingsControl}'s {@link SettingsPanel} to navigate up one level in
       
    95      * the navigation stack.
       
    96      */
       
    97     protected void addDefaultBackAction() {
       
    98 	getComponent().getButtonBar().getBackButton().addActionListener(
       
    99 	    new ActionListener() {
       
   100 		@Override
       
   101 		public void actionPerformed(ActionEvent e) {
       
   102 		    getNavigator().goToAsync(false, SettingsControl.this,
       
   103 			Navigator.PARENT_NAVIGABLE);
       
   104 		}
       
   105 	    });
       
   106     }
       
   107 
       
   108     /**
       
   109      * Adds a listener to the <strong>Cancel</strong> button of this {@code
       
   110      * SettingsControl}'s {@link SettingsPanel} to call {@link #doQuit}, if
       
   111      * {@code quit} is {@code true}, or {@link #doCancel}, if {@code quit} is
       
   112      * {@code false}.
       
   113      */
       
   114     protected void addDefaultCancelAction(final boolean quit) {
       
   115 	getComponent().getButtonBar().getCancelButton().addActionListener(
       
   116 	    new ActionListener() {
       
   117 		@Override
       
   118 		public void actionPerformed(ActionEvent e) {
       
   119 		    if (quit) {
       
   120 			doQuit();
       
   121 		    } else {
       
   122 			doCancel();
       
   123 		    }
       
   124 		}
       
   125 	    });
       
   126     }
       
   127 
       
   128     /**
       
   129      * Adds a listener to the <strong>Close</strong> button of this {@code
       
   130      * SettingsControl}'s {@link SettingsPanel} to call {@link
       
   131      * #doSaveAndQuit}, if {@code quit} is {@code true}, or {@link #doOkay}, if
       
   132      * {@code quit} is {@code false}.
       
   133      */
       
   134     protected void addDefaultCloseAction(final boolean quit) {
       
   135 	getComponent().getButtonBar().getCloseButton().addActionListener(
       
   136 	    new ActionListener() {
       
   137 		@Override
       
   138 		public void actionPerformed(ActionEvent e) {
       
   139 		    if (quit) {
       
   140 			doSaveAndQuit();
       
   141 		    } else {
       
   142 			doOkay();
       
   143 		    }
       
   144 		}
       
   145 	    });
       
   146     }
       
   147 
       
   148     /**
       
   149      * Adds a listener to the <strong>Help</strong> button of this {@code
       
   150      * SettingsControl}'s {@link SettingsPanel} to show this {@code
       
   151      * DefaultControl}'s {@link DefaultControl#getHelpURL help}.
       
   152      *
       
   153      * @param	    topmost {@code false} to display the help from this {@link
       
   154      *		    Control}, {@code true} to display the help from the topmost
       
   155      *		    {@link Control} in the navigation stack
       
   156      */
       
   157     protected void addDefaultHelpAction(final boolean topmost) {
       
   158 	getComponent().getButtonBar().getHelpButton().addActionListener(
       
   159 	    new ActionListener() {
       
   160 		@Override
       
   161 		public void actionPerformed(ActionEvent e) {
       
   162 		    URL url = null;
       
   163 
       
   164 		    if (topmost) {
       
   165 			List<Control> controls = getNavigator().getPath();
       
   166 			for (int i = controls.size() - 1; i >= 0; i--) {
       
   167 			    Control control = controls.get(i);
       
   168 			    url = control.getHelpURL();
       
   169 			    if (url != null) {
       
   170 				break;
       
   171 			    }
       
   172 			}
       
   173 		    } else {
       
   174 			url = getHelpURL();
       
   175 		    }
       
   176 
       
   177 		    if (url != null) {
       
   178 			ClientContext context = getClientContext();
       
   179 			context.getHelpBroker().setCurrentURL(url);
       
   180 			context.showHelp();
       
   181 		    }
       
   182 		}
       
   183 	    });
       
   184     }
       
   185 
       
   186     /**
       
   187      * Adds a listener to the <strong>Okay</strong> button of this {@code
       
   188      * SettingsControl}'s {@link SettingsPanel} to call {@link
       
   189      * #doSaveAndQuit}, if {@code quit} is {@code true}, or {@link #doOkay}, if
       
   190      * {@code quit} is {@code false}.
       
   191      */
       
   192     protected void addDefaultOkayAction(final boolean quit) {
       
   193 	getComponent().getButtonBar().getOkayButton().addActionListener(
       
   194 	    new ActionListener() {
       
   195 		@Override
       
   196 		public void actionPerformed(ActionEvent e) {
       
   197 		    if (quit) {
       
   198 			doSaveAndQuit();
       
   199 		    } else {
       
   200 			doOkay();
       
   201 		    }
       
   202 		}
       
   203 	    });
       
   204     }
       
   205 
       
   206     /**
       
   207      * Adds a listener to the <strong>Quit</strong> button of this {@code
       
   208      * SettingsControl}'s {@link SettingsPanel} to close this instance.
       
   209      */
       
   210     protected void addDefaultQuitAction() {
       
   211 	getComponent().getButtonBar().getQuitButton().addActionListener(
       
   212 	    new ActionListener() {
       
   213 		@Override
       
   214 		public void actionPerformed(ActionEvent e) {
       
   215 		    doQuit();
       
   216 		}
       
   217 	    });
       
   218     }
       
   219 
       
   220     /**
       
   221      * Adds a listener to the <strong>Reset</strong> button of this {@code
       
   222      * SettingsControl}'s {@link SettingsPanel} to invoke this {@link
       
   223      * Control}'s reset action.
       
   224      */
       
   225     protected void addDefaultResetAction() {
       
   226 	getComponent().getButtonBar().getResetButton().addActionListener(
       
   227 	    new ActionListener() {
       
   228 		@Override
       
   229 		public void actionPerformed(ActionEvent e) {
       
   230 		    getResetAction().asyncInvoke();
       
   231 		}
       
   232 	    });
       
   233     }
       
   234 
       
   235     /**
       
   236      * Asynchronously closes this instance.
       
   237      */
       
   238     public void doQuit() {
       
   239 	final StructuredAction<?, ?, ?> resetAction = getResetAction();
       
   240 	resetAction.asyncExec(
       
   241 	    new Runnable() {
       
   242 		@Override
       
   243 		public void run() {
       
   244 		    try {
       
   245 			getClientContext().closeInstance(true);
       
   246 		    } catch (ActionException ignore) {
       
   247 		    }
       
   248 		}
       
   249 	    });
       
   250     }
       
   251 
       
   252     /**
       
   253      * Asynchronously invokes this {@link Control}'s save action, then closes
       
   254      * this instance.
       
   255      */
       
   256     public void doSaveAndQuit() {
       
   257 	final StructuredAction<?, ?, ?> saveAction = getSaveAction();
       
   258 	saveAction.asyncExec(
       
   259 	    new Runnable() {
       
   260 		@Override
       
   261 		public void run() {
       
   262 		    try {
       
   263 			saveAction.invoke();
       
   264 			getClientContext().closeInstance(false);
       
   265 		    } catch (ActionException ignore) {
       
   266 		    }
       
   267 		}
       
   268 	    });
       
   269     }
       
   270 }