001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.metadata.range.swing.rolehandling;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.net.*;
024: import java.rmi.*;
025:
026: import javax.swing.*;
027: import javax.xml.rpc.*;
028:
029: import org.openharmonise.him.*;
030: import org.openharmonise.him.context.StateHandler;
031: import org.openharmonise.him.harmonise.*;
032: import org.openharmonise.him.metadata.range.swing.*;
033: import org.openharmonise.him.metadata.range.swing.valuehandling.*;
034: import org.openharmonise.him.window.messages.*;
035: import org.openharmonise.vfs.authentication.*;
036: import org.openharmonise.vfs.context.*;
037: import org.openharmonise.vfs.metadata.*;
038: import org.openharmonise.vfs.servers.*;
039:
040: /**
041: *
042: * @author Matthew Large
043: * @version $Revision: 1.2 $
044: *
045: */
046: public class RoleRangeDisplay extends AbstractRangeDisplay implements
047: RangeDisplay, LayoutManager, ActionListener, Runnable {
048:
049: private ValueRangeDisplay m_valueDisplay = null;
050:
051: private JCheckBox m_super CheckBox = null;
052:
053: private JLabel m_super Label = null;
054:
055: private static final String WAIT_LABEL = "SET_SUPER-ACTION";
056:
057: /**
058: * @param propInstance
059: */
060: public RoleRangeDisplay(PropertyInstance propInstance) {
061: super (propInstance);
062: this .setup();
063: }
064:
065: private void setup() {
066: this .setLayout(this );
067: this .m_valueDisplay = new ValueRangeDisplay(super
068: .getPropertyInstance());
069: this .add(m_valueDisplay.getPanel());
070:
071: this .m_super CheckBox = new JCheckBox();
072: this .m_super CheckBox.setActionCommand("SUPER_CHECK");
073: this .m_super CheckBox.addActionListener(this );
074: if (this .isSuperUser()) {
075: m_super CheckBox.setSelected(true);
076: this .m_valueDisplay.setEnabled(false);
077: }
078: this .add(this .m_super CheckBox);
079:
080: String fontName = "Dialog";
081: int fontSize = 11;
082: Font font = new Font(fontName, Font.PLAIN, fontSize);
083:
084: this .m_super Label = new JLabel("Super User");
085: this .m_super Label.setFont(font);
086: this .add(this .m_super Label);
087:
088: SwingUtilities.invokeLater(this );
089: }
090:
091: /**
092: * Set the super user flag for the User currently being edited.
093: * @param bIsSuper
094: */
095: private void setIsSuperUser(boolean bIsSuper) {
096: Server server = null;
097: server = ServerList.getInstance().getHarmoniseServer();
098: URI uri = server.getURI();
099:
100: String sURI = uri.getScheme() + "://" + uri.getHost() + ":"
101: + uri.getPort() + "/webdav/services/HarmoniseService";
102: URL url = null;
103: try {
104: url = new URL(sURI);
105: } catch (MalformedURLException e2) {
106: e2.printStackTrace();
107: System.exit(1);
108: }
109:
110: AuthInfo auth = server.getVFS().getAuthentication();
111:
112: try {
113: UserConfigClient.setIsSuperUser(url, auth.getUsername(),
114: auth.getPassword(), this .getPropertyInstance()
115: .getVirtualFile().getFileName(), bIsSuper);
116: if (bIsSuper) {
117: MessageHandler
118: .getInstance()
119: .fireMessageEvent(
120: "User \""
121: + this
122: .getPropertyInstance()
123: .getVirtualFile()
124: .getVFS()
125: .getVirtualFileSystemView()
126: .getDisplayName(
127: this
128: .getPropertyInstance()
129: .getVirtualFile())
130: + "\" is now a Super User.",
131: MessageHandler.TYPE_CONFIRM);
132: } else {
133: MessageHandler
134: .getInstance()
135: .fireMessageEvent(
136: "User \""
137: + this
138: .getPropertyInstance()
139: .getVirtualFile()
140: .getVFS()
141: .getVirtualFileSystemView()
142: .getDisplayName(
143: this
144: .getPropertyInstance()
145: .getVirtualFile())
146: + "\" is not a Super User anymore.",
147: MessageHandler.TYPE_CONFIRM);
148: }
149: } catch (RemoteException e) {
150: e.printStackTrace();
151: MessageHandler
152: .getInstance()
153: .fireMessageEvent(
154: "Error changing Super User status for user \""
155: + this
156: .getPropertyInstance()
157: .getVirtualFile()
158: .getVFS()
159: .getVirtualFileSystemView()
160: .getDisplayName(
161: this
162: .getPropertyInstance()
163: .getVirtualFile())
164: + "\".There was a problem communicating with the server.",
165: MessageHandler.TYPE_ERROR);
166: } catch (ServiceException e) {
167: MessageHandler
168: .getInstance()
169: .fireMessageEvent(
170: "Error changing Super User status for user \""
171: + this
172: .getPropertyInstance()
173: .getVirtualFile()
174: .getVFS()
175: .getVirtualFileSystemView()
176: .getDisplayName(
177: this
178: .getPropertyInstance()
179: .getVirtualFile())
180: + "\".There was a problem communicating with the server.",
181: MessageHandler.TYPE_ERROR);
182: e.printStackTrace();
183: }
184: }
185:
186: /**
187: * Check whether the User curently being edited is a super user
188: * @return true or false
189: */
190: private boolean isSuperUser() {
191: boolean bRetn = false;
192: Server server = null;
193: server = ServerList.getInstance().getHarmoniseServer();
194: URI uri = server.getURI();
195:
196: String sURI = uri.getScheme() + "://" + uri.getHost() + ":"
197: + uri.getPort() + "/webdav/services/HarmoniseService";
198: URL url = null;
199: try {
200: url = new URL(sURI);
201: } catch (MalformedURLException e2) {
202: e2.printStackTrace();
203: System.exit(1);
204: }
205:
206: AuthInfo auth = server.getVFS().getAuthentication();
207:
208: try {
209: if (UserConfigClient.isSuperUser(url, auth.getUsername(),
210: auth.getPassword(), getPropertyInstance()
211: .getVirtualFile().getFileName())) {
212: bRetn = true;
213: }
214: } catch (RemoteException e) {
215: e.printStackTrace();
216: } catch (ServiceException e) {
217: e.printStackTrace();
218: }
219: return bRetn;
220: }
221:
222: /* (non-Javadoc)
223: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
224: */
225: public void actionPerformed(ActionEvent ae) {
226: if (ae.getActionCommand().equals("SUPER_CHECK")) {
227: boolean bSelected = this .m_super CheckBox.isSelected();
228: this .m_valueDisplay.setEnabled(!bSelected);
229: StateHandler.getInstance().addWait(WAIT_LABEL);
230: this .setIsSuperUser(bSelected);
231: StateHandler.getInstance().removeWait(WAIT_LABEL);
232: }
233: }
234:
235: /* (non-Javadoc)
236: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isMetadataValid()
237: */
238: public boolean isMetadataValid() {
239: return this .m_valueDisplay.isMetadataValid();
240: }
241:
242: /* (non-Javadoc)
243: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
244: */
245: public JPanel getPanel() {
246: return this ;
247: }
248:
249: /* (non-Javadoc)
250: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
251: */
252: public void layoutContainer(Container arg0) {
253: this .m_valueDisplay.setSize(this .m_valueDisplay
254: .getPreferredSize());
255: this .m_valueDisplay.setLocation(0, 0);
256:
257: this .m_super CheckBox.setSize(this .m_super CheckBox
258: .getPreferredSize());
259: this .m_super CheckBox.setLocation(20, this .m_valueDisplay
260: .getHeight() + 10);
261:
262: this .m_super Label.setSize(this .m_super Label.getPreferredSize());
263: this .m_super Label.setLocation(22 + this .m_super CheckBox
264: .getWidth(), this .m_valueDisplay.getHeight() + 13);
265: }
266:
267: /* (non-Javadoc)
268: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
269: */
270: public Dimension minimumLayoutSize(Container arg0) {
271: return this .m_valueDisplay.minimumLayoutSize(arg0);
272: }
273:
274: /* (non-Javadoc)
275: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
276: */
277: public Dimension preferredLayoutSize(Container arg0) {
278: return this .m_valueDisplay.preferredLayoutSize(arg0);
279: }
280:
281: /* (non-Javadoc)
282: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
283: */
284: public void removeLayoutComponent(Component arg0) {
285: // NO-OP
286: }
287:
288: /* (non-Javadoc)
289: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
290: */
291: public void addLayoutComponent(String arg0, Component arg1) {
292: // NO-OP
293: }
294:
295: /* (non-Javadoc)
296: * @see java.awt.Component#getPreferredSize()
297: */
298: public Dimension getPreferredSize() {
299: int nWidth = this .getParent().getWidth() - 40;
300: int nHeight = 200;
301: return new Dimension(nWidth, nHeight);
302: }
303:
304: /* (non-Javadoc)
305: * @see java.lang.Runnable#run()
306: */
307: public void run() {
308: if (this .isSuperUser()) {
309: m_super CheckBox.setSelected(true);
310: this .m_valueDisplay.setEnabled(false);
311: }
312: }
313:
314: }
|