usr/src/java/vpanels/app/usermgr/com/oracle/solaris/vp/panels/usermgr/client/swing/FilterUserAction.java
changeset 847 a8e124b894b8
equal deleted inserted replaced
846:0a2af4721353 847:a8e124b894b8
       
     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) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.usermgr.client.swing;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import javax.swing.*;
       
    31 import java.util.List;
       
    32 import javax.swing.text.JTextComponent;
       
    33 import com.oracle.solaris.vp.panel.common.action.*;
       
    34 import com.oracle.solaris.vp.panel.common.control.*;
       
    35 import com.oracle.solaris.vp.panel.swing.action.SwingManagedObjectAction;
       
    36 import com.oracle.solaris.rad.usermgr.*;
       
    37 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    38 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    39 import com.oracle.solaris.vp.util.misc.property.*;
       
    40 import com.oracle.solaris.vp.util.swing.*;
       
    41 import com.oracle.solaris.vp.util.swing.layout.*;
       
    42 import com.oracle.solaris.vp.util.swing.property.*;
       
    43 
       
    44 /**
       
    45  * Filter users based on the scope (files or ldap)
       
    46  * and type (user or role). The search string provides
       
    47  * additional filtering.
       
    48  */
       
    49 @SuppressWarnings({"serial"})
       
    50 public class FilterUserAction extends SwingManagedObjectAction
       
    51     <UserManagedObject, FilterUserAction.Data, UserManagedObject> {
       
    52 
       
    53     private UserManagedObject umo = null;
       
    54     private UserImpl user = null;
       
    55 
       
    56     //
       
    57     // Inner classes
       
    58     //
       
    59 
       
    60     protected class Data { // implements ActionListener
       
    61 	//
       
    62 	// Instance data
       
    63 	//
       
    64 
       
    65 	private ButtonGroup scopeGroup;
       
    66 	private ButtonGroup typeGroup;
       
    67 	private JTextField matchField;
       
    68 	private JOptionPane pane;
       
    69 	private JDialog dialog;
       
    70 
       
    71 	private UserMgrPanelDescriptor descriptor = null;
       
    72 
       
    73 	public MutableProperty<String> matchProperty = new StringProperty();
       
    74 	public MutableProperty<String> typeProperty = new StringProperty();
       
    75 
       
    76 
       
    77 	//
       
    78 	// Constructors
       
    79 	//
       
    80 
       
    81 	public Data()   {
       
    82 	    pane = new JOptionPane(CreateGui(),
       
    83 		JOptionPane.PLAIN_MESSAGE,
       
    84 		JOptionPane.OK_CANCEL_OPTION);
       
    85 	    UserMgrUtils.removeIcons(pane);
       
    86 
       
    87 	    dialog = pane.createDialog(getHasComponent().getComponent(),
       
    88 		Finder.getString("usermgr.filter.title"));
       
    89 	}
       
    90 
       
    91         private JPanel CreateGui() {
       
    92 	    ActionListener listener =
       
    93 		new ActionListener() {
       
    94 		    @Override
       
    95 		    public void actionPerformed(ActionEvent e) {
       
    96 			pane.setValue(JOptionPane.OK_OPTION);
       
    97 		    }
       
    98 		};
       
    99 
       
   100 	ActionString actString = null;
       
   101 	JRadioButton scopeButton = null;
       
   102 	descriptor = control.getPanelDescriptor();
       
   103 	JPanel form = new JPanel(new GridBagLayout());
       
   104 	GridBagConstraints gbc = new GridBagConstraints();
       
   105 	int width = GUIUtil.getTextFieldWidth();
       
   106 
       
   107 	gbc.gridx = 0;
       
   108 	gbc.gridy = 0;
       
   109 	// gbc.weightx = 1.0;
       
   110 	gbc.fill = GridBagConstraints.REMAINDER;
       
   111 	gbc.anchor = GridBagConstraints.LINE_START;
       
   112         int hGap = GUIUtil.getHalfGap();
       
   113 	gbc.insets = new Insets(0, 0, hGap, hGap);
       
   114 
       
   115 	// Label for the dialog
       
   116 	JLabel introLabel = new JLabel(
       
   117 	    Finder.getString("usermgr.filter.label"));
       
   118 	form.add(introLabel, gbc);
       
   119 
       
   120 	//
       
   121 	// Scope: files or ldap
       
   122 	//
       
   123         JLabel scopeLabel = new JLabel(
       
   124 		Finder.getString("usermgr.filter.scope"));
       
   125 	JPanel scopePanel = new JPanel();
       
   126 
       
   127 	scopeGroup = new ButtonGroup();
       
   128 	List<String> scopes = descriptor.getScopes();
       
   129 	JRadioButton[] buttons = new JRadioButton[scopes.size()];
       
   130 	for (int i = 0; i < scopes.size(); i++) {
       
   131 		String scope = scopes.get(i);
       
   132 		scopeButton = new JRadioButton(scope);
       
   133 		scopeButton.setActionCommand(scope);
       
   134 		int mnemonic = scope.toUpperCase().charAt(0);
       
   135 		scopeButton.setMnemonic(mnemonic);
       
   136 		scopeGroup.add(scopeButton);
       
   137 		scopePanel.add(scopeButton);
       
   138 		buttons[i] = scopeButton;
       
   139 	}
       
   140 
       
   141 	// For single scope, disable the button
       
   142 	if (scopes.size() == 1) {
       
   143 	    buttons[0].setEnabled(false);
       
   144 	}
       
   145 
       
   146 	gbc.gridy++;
       
   147 	gbc.gridwidth = 1;
       
   148 	gbc.fill = GridBagConstraints.NONE;
       
   149         form.add(scopeLabel, gbc);
       
   150 	gbc.fill = GridBagConstraints.REMAINDER;
       
   151 	gbc.gridx = GridBagConstraints.RELATIVE;
       
   152         form.add(scopePanel, gbc);
       
   153 
       
   154 	//
       
   155 	// Type: User or Role
       
   156 	//
       
   157         JLabel typeLabel = new JLabel(
       
   158 		Finder.getString("usermgr.filter.type"));
       
   159 	JPanel typePanel = new JPanel();
       
   160 
       
   161 	typeGroup = new ButtonGroup();
       
   162 	actString = new ActionString("usermgr.filter.type.user");
       
   163 	JRadioButton userButton = new JRadioButton(actString.getString());
       
   164 	userButton.setMnemonic(actString.getMnemonic());
       
   165         userButton.setSelected(true);
       
   166         userButton.setActionCommand("normal");
       
   167 	typeGroup.add(userButton);
       
   168 	typePanel.add(userButton);
       
   169 
       
   170 	actString = new ActionString("usermgr.filter.type.role");
       
   171 	JRadioButton roleButton = new JRadioButton(actString.getString());
       
   172 	roleButton.setMnemonic(actString.getMnemonic());
       
   173         roleButton.setActionCommand("role");
       
   174 	typeGroup.add(roleButton);
       
   175 	typePanel.add(roleButton);
       
   176 
       
   177 	gbc.gridy++;
       
   178 	gbc.gridwidth = 1;
       
   179 	gbc.fill = GridBagConstraints.NONE;
       
   180         form.add(typeLabel, gbc);
       
   181 	gbc.fill = GridBagConstraints.REMAINDER;
       
   182 	gbc.gridx = GridBagConstraints.RELATIVE;
       
   183         form.add(typePanel, gbc);
       
   184 
       
   185 	// Match String
       
   186 	actString = new ActionString("usermgr.filter.match");
       
   187 	JLabel matchLabel = new JLabel(actString.getString());
       
   188 	matchLabel.setDisplayedMnemonic(actString.getMnemonic());
       
   189 	gbc.gridx = 0;
       
   190 	gbc.gridy++; // next row
       
   191         form.add(matchLabel, gbc);
       
   192 
       
   193 	matchField = GUIUtil.createTextField();
       
   194 	matchField.addActionListener(listener);
       
   195 	new TextComponentPropertySynchronizer<String, JTextComponent>
       
   196 		(matchProperty, matchField, false);
       
   197 
       
   198 	matchLabel.setLabelFor(matchField);
       
   199 	gbc.gridx = GridBagConstraints.RELATIVE;
       
   200         form.add(matchField, gbc);
       
   201 
       
   202 	//
       
   203 	// Start with previously selected filter entries
       
   204 	//
       
   205 	if (userButton.getActionCommand().equals(descriptor.getType())) {
       
   206 	    userButton.setSelected(true);
       
   207 	} else {
       
   208 	    roleButton.setSelected(true);
       
   209 	}
       
   210 	matchField.setText(descriptor.getMatch());
       
   211 
       
   212 	for (int j = 0; j < buttons.length; j++) {
       
   213 	    if (buttons[j].getActionCommand().equals(descriptor.getScope())) {
       
   214 		buttons[j].setSelected(true);
       
   215 		break;
       
   216 	    }
       
   217 	}
       
   218 
       
   219 	return form;
       
   220     }
       
   221 
       
   222     public String getScope() {
       
   223     	ButtonModel bm = scopeGroup.getSelection();
       
   224 	return bm.getActionCommand();
       
   225     }
       
   226 
       
   227     public String getType() {
       
   228     	ButtonModel bm = typeGroup.getSelection();
       
   229 	return bm.getActionCommand();
       
   230     }
       
   231 
       
   232     public String getString() {
       
   233 	return matchField.getText();
       
   234     }
       
   235     }
       
   236 
       
   237     //
       
   238     // Static data
       
   239     //
       
   240 
       
   241     private static final String FILTER_TEXT = Finder.getString(
       
   242 	"usermgr.action.filter.button");
       
   243 
       
   244     //
       
   245     // Instance data
       
   246     //
       
   247 
       
   248     private MainControl control;
       
   249 
       
   250     //
       
   251     // Constructors
       
   252     //
       
   253 
       
   254     public FilterUserAction(MainControl control) {
       
   255 	super(FILTER_TEXT, null, control);
       
   256         ActionString actStr = new ActionString(
       
   257 	    "usermgr.action.filter.button");
       
   258 	putValue(Action.NAME, actStr.getString());
       
   259 	putValue(Action.MNEMONIC_KEY, actStr.getMnemonic());
       
   260 	// putValue(Action.MNEMONIC_KEY, KeyEvent.VK_F);
       
   261 	this.control = control;
       
   262 	setLoops(true);
       
   263     }
       
   264 
       
   265     //
       
   266     // StructuredAction methods
       
   267     //
       
   268 
       
   269     // @Override
       
   270     public Data getRuntimeInput(List<UserManagedObject> selection,
       
   271     	Data uData) throws ActionAbortedException {
       
   272 
       
   273 	// Since this action navigates to the new object once created, we must
       
   274 	// first navigate to control so that any outstanding changes in the
       
   275 	// current Control are handled.
       
   276 	try {
       
   277 	    control.getNavigator().goToAsyncAndWait(false, control);
       
   278 	} catch (NavigationException e) {
       
   279 	    throw new ActionAbortedException(e);
       
   280 	}
       
   281 
       
   282 	if (uData == null) {
       
   283 	    uData = new Data();
       
   284 	}
       
   285 
       
   286 	// Blocks until dismissed
       
   287 	uData.dialog.setVisible(true);
       
   288 
       
   289 	if (!ObjectUtil.equals(uData.pane.getValue(), JOptionPane.OK_OPTION)) {
       
   290 	    throw new ActionAbortedException();
       
   291 	}
       
   292 
       
   293 	return uData;
       
   294     }
       
   295 
       
   296     //
       
   297     // DefaultStructuredAction methods
       
   298     //
       
   299 
       
   300     // @Override
       
   301     public UserManagedObject workBusy(List<UserManagedObject> selection,
       
   302 	Data uData) throws ActionFailedException,
       
   303 		ActionAbortedException,
       
   304 		ActionUnauthorizedException {
       
   305 
       
   306 	UserMgrPanelDescriptor descriptor = control.getPanelDescriptor();
       
   307 	descriptor.initUsers(uData.getScope(),
       
   308 		uData.getType(), uData.getString());
       
   309 
       
   310 	return null; // umo;
       
   311     }
       
   312 
       
   313 }