01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.readmetool;
11:
12: import org.eclipse.jface.action.ControlContribution;
13: import org.eclipse.swt.SWT;
14: import org.eclipse.swt.widgets.Composite;
15: import org.eclipse.swt.widgets.Control;
16: import org.eclipse.swt.widgets.Label;
17: import org.eclipse.ui.IEditorPart;
18: import org.eclipse.ui.IPropertyListener;
19:
20: /**
21: * This class demonstrates the contribution of a custom control to the
22: * status line for the readme editor. The control shows the
23: * dirty status of the editor.
24: */
25: public class DirtyStateContribution extends ControlContribution
26: implements IPropertyListener {
27: private Composite composite;
28:
29: private Label label;
30:
31: private IEditorPart activeEditor;
32:
33: /**
34: * Creates a new DirtyStateContribution.
35: */
36: protected DirtyStateContribution() {
37: super ("DirtyState"); //$NON-NLS-1$
38: }
39:
40: /* (non-Javadoc)
41: * Method declared on ControlContribution
42: */
43: protected Control createControl(Composite parent) {
44: // If the composite is good just return it.
45: if (composite != null && !composite.isDisposed())
46: return composite;
47:
48: // Create composite for border.
49: composite = new Composite(parent, SWT.BORDER);
50: composite.setData(this );
51:
52: // Create label inside composite.
53: label = new Label(composite, SWT.NONE);
54: label.setSize(80, 15);
55:
56: updateState();
57: return composite;
58: }
59:
60: /**
61: * Called when an editor is activated.
62: *
63: * @see ReadmeEditorActionBarContributor#setActiveEditor(IEditorPart)
64: */
65: public void editorChanged(IEditorPart part) {
66: if (activeEditor != null) {
67: activeEditor.removePropertyListener(this );
68: }
69: activeEditor = part;
70: if (activeEditor != null) {
71: activeEditor.addPropertyListener(this );
72: }
73: updateState();
74: }
75:
76: /* (non-Javadoc)
77: * Method declared on IPropertyListener
78: */
79: public void propertyChanged(Object source, int propID) {
80: if (source instanceof IEditorPart)
81: updateState();
82: }
83:
84: /**
85: * Updates the state of the label.
86: */
87: private void updateState() {
88: if (label == null || label.isDisposed())
89: return;
90:
91: boolean saveNeeded = false;
92: if (activeEditor != null)
93: saveNeeded = activeEditor.isDirty();
94: if (saveNeeded)
95: label.setText(MessageUtil.getString("Save_Needed")); //$NON-NLS-1$
96: else
97: label.setText(MessageUtil.getString("Clean")); //$NON-NLS-1$
98: }
99: }
|