001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.components.views;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.jface.dialogs.ErrorDialog;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.ModifyListener;
020: import org.eclipse.swt.events.SelectionAdapter;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.events.SelectionListener;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Button;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.FileDialog;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.swt.widgets.Text;
030: import org.eclipse.ui.internal.components.framework.ComponentException;
031: import org.eclipse.ui.internal.part.components.services.INameable;
032: import org.eclipse.ui.internal.part.components.services.IStatusFactory;
033:
034: /**
035: * Example view that contains text fields allowing the user to change the view's
036: * title, icon, tooltip, and content description
037: *
038: * @since 3.1
039: */
040: public class NameTestView {
041: // Dependencies
042: private INameable name;
043: private IStatusFactory error;
044:
045: // Widgets
046: private Composite mainControl;
047: private Text nameField;
048: private Text contentField;
049: private Text tooltip;
050: private Button selectImage;
051:
052: // Listeners
053: private ModifyListener modifyListener = new ModifyListener() {
054: public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
055: textChanged((Text) e.widget);
056: }
057: };
058:
059: private SelectionListener buttonListener = new SelectionAdapter() {
060: /* (non-Javadoc)
061: * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
062: */
063: public void widgetSelected(SelectionEvent e) {
064: buttonPressed((Button) e.widget);
065: }
066: };
067:
068: /**
069: * Creates a new NameTestView that takes the parent composite,
070: */
071: public NameTestView(Composite parent, INameable name,
072: IStatusFactory error) throws ComponentException {
073:
074: // Remember our dependencies
075: this .name = name;
076: this .mainControl = parent;
077: this .error = error;
078:
079: // Construct the widgets and the layout
080: GridLayout layout = new GridLayout();
081: layout.numColumns = 2;
082: parent.setLayout(layout);
083:
084: // Create text fields for editing the view name, description, and tooltip
085: createLabel(parent, "Name");
086: nameField = createTextField(parent);
087: createLabel(parent, "Description");
088: contentField = createTextField(parent);
089: createLabel(parent, "Tooltip");
090: tooltip = createTextField(parent);
091:
092: // Add a button that allows a new image to be selected
093: createLabel(parent, "Image");
094: selectImage = createButton(parent, "Select Image");
095: }
096:
097: /**
098: * Called whenever a Button is pressed
099: *
100: * @param button that was pressed
101: */
102: protected void buttonPressed(Button button) {
103: if (button == selectImage) {
104: FileDialog dialog = new FileDialog(mainControl.getShell(),
105: SWT.OPEN);
106:
107: String filename = dialog.open();
108: if (filename != null) {
109: try {
110: // Create an image descriptor for the selected file
111: ImageDescriptor image = ImageDescriptor
112: .createFromURL(new URL("file:/" + filename));
113:
114: // Change the title image
115: name.setImage(image);
116: } catch (MalformedURLException e) {
117: IStatus status = error.newError(
118: "Unable to open image", e);
119: ErrorDialog.openError(mainControl.getShell(),
120: "Error opening file", null, status);
121: return;
122: }
123: }
124: }
125: }
126:
127: /**
128: * Called whenever a Text field changes
129: *
130: * @param changed Text field that changed
131: */
132: private void textChanged(Text changed) {
133: if (changed == nameField) {
134: name.setName(nameField.getText());
135: } else if (changed == contentField) {
136: name.setContentDescription(contentField.getText());
137: } else if (changed == tooltip) {
138: name.setTooltip(tooltip.getText());
139: }
140: }
141:
142: // Widget factory methods ///////////////////////////////////////////
143:
144: private Text createTextField(Composite parent) {
145: Text result = new Text(parent, SWT.BORDER);
146: result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
147: | GridData.VERTICAL_ALIGN_CENTER));
148: result.addModifyListener(modifyListener);
149: return result;
150: }
151:
152: private Button createButton(Composite parent, String buttonText) {
153: Button newButton = new Button(parent, SWT.PUSH);
154: newButton.setText(buttonText);
155: newButton.addSelectionListener(buttonListener);
156: newButton.setLayoutData(new GridData(
157: GridData.HORIZONTAL_ALIGN_BEGINNING
158: | GridData.VERTICAL_ALIGN_CENTER));
159: return newButton;
160: }
161:
162: private Label createLabel(Composite parent, String labelText) {
163: Label label = new Label(parent, SWT.NONE);
164: label.setText(labelText);
165: label.setLayoutData(new GridData(
166: GridData.HORIZONTAL_ALIGN_BEGINNING
167: | GridData.VERTICAL_ALIGN_CENTER));
168: return label;
169: }
170: }
|