001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.studio.apacheds.actions;
021
022
023 import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
024 import org.apache.directory.studio.apacheds.model.Server;
025 import org.apache.directory.studio.apacheds.views.ServersView;
026 import org.eclipse.jface.action.Action;
027 import org.eclipse.jface.action.IAction;
028 import org.eclipse.jface.preference.PreferenceDialog;
029 import org.eclipse.jface.viewers.ISelection;
030 import org.eclipse.jface.viewers.StructuredSelection;
031 import org.eclipse.ui.IWorkbenchWindow;
032 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
033 import org.eclipse.ui.dialogs.PreferencesUtil;
034
035
036 /**
037 * This class implements the properties action for a server.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 * @version $Rev$, $Date$
041 */
042 public class PropertiesAction extends Action implements IWorkbenchWindowActionDelegate
043 {
044 /** The associated view */
045 private ServersView view;
046
047
048 /**
049 * Creates a new instance of PropertiesAction.
050 */
051 public PropertiesAction()
052 {
053 super( Messages.getString( "PropertiesAction.Properties" ) ); //$NON-NLS-1$
054 init();
055 }
056
057
058 /**
059 * Creates a new instance of PropertiesAction.
060 *
061 * @param view
062 * the associated view
063 */
064 public PropertiesAction( ServersView view )
065 {
066 super( Messages.getString( "PropertiesAction.Properties" ) ); //$NON-NLS-1$
067 this.view = view;
068 init();
069 }
070
071
072 /**
073 * Initializes the action.
074 */
075 private void init()
076 {
077 setId( ApacheDsPluginConstants.CMD_PROPERTIES );
078 setActionDefinitionId( ApacheDsPluginConstants.CMD_PROPERTIES );
079 setToolTipText( Messages.getString( "PropertiesAction.PropertiesToolTip" ) ); //$NON-NLS-1$
080 }
081
082
083 /* (non-Javadoc)
084 * @see org.eclipse.jface.action.Action#run()
085 */
086 public void run()
087 {
088 if ( view != null )
089 {
090 StructuredSelection selection = ( StructuredSelection ) view.getViewer().getSelection();
091 if ( !selection.isEmpty() )
092 {
093 Server server = ( Server ) selection.getFirstElement();
094 PreferenceDialog dialog = PreferencesUtil.createPropertyDialogOn( view.getViewSite().getShell(),
095 server, ApacheDsPluginConstants.PROP_SERVER_PROPERTY_PAGE, null, null );
096 dialog.getShell().setText(
097 Messages.getString( "PropertiesAction.PropertiesFor" ) + shorten( server.getName(), 30 ) + "'" ); //$NON-NLS-1$ //$NON-NLS-2$
098 dialog.open();
099 }
100 }
101 }
102
103
104 /**
105 * Shortens the given label to the given maximum length
106 * and filters non-printable characters.
107 *
108 * @param label the label
109 * @param maxLength the max length
110 *
111 * @return the shortened label
112 */
113 public static String shorten( String label, int maxLength )
114 {
115 if ( label == null )
116 {
117 return null;
118 }
119
120 // shorten label
121 if ( maxLength < 3 )
122 {
123 return "..."; //$NON-NLS-1$
124 }
125 if ( label.length() > maxLength )
126 {
127 label = label.substring( 0, maxLength / 2 ) + "..." //$NON-NLS-1$
128 + label.substring( label.length() - maxLength / 2, label.length() );
129
130 }
131
132 // filter non-printable characters
133 StringBuffer sb = new StringBuffer( maxLength + 3 );
134 for ( int i = 0; i < label.length(); i++ )
135 {
136 char c = label.charAt( i );
137 if ( Character.isISOControl( c ) )
138 {
139 sb.append( '.' );
140 }
141 else
142 {
143 sb.append( c );
144 }
145 }
146
147 return sb.toString();
148 }
149
150
151 /* (non-Javadoc)
152 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
153 */
154 public void run( IAction action )
155 {
156 run();
157 }
158
159
160 /* (non-Javadoc)
161 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
162 */
163 public void dispose()
164 {
165 // Nothing to do
166 }
167
168
169 /* (non-Javadoc)
170 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
171 */
172 public void init( IWorkbenchWindow window )
173 {
174 // Nothing to do
175 }
176
177
178 /* (non-Javadoc)
179 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
180 */
181 public void selectionChanged( IAction action, ISelection selection )
182 {
183 // Nothing to do
184 }
185 }