001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.navigator.actions;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.lang.reflect.InvocationTargetException;
016: import java.util.Properties;
017:
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IProgressMonitor;
022: import org.eclipse.jface.action.IAction;
023: import org.eclipse.jface.dialogs.MessageDialog;
024: import org.eclipse.jface.viewers.ISelection;
025: import org.eclipse.jface.viewers.IStructuredSelection;
026: import org.eclipse.jface.viewers.StructuredSelection;
027: import org.eclipse.swt.widgets.Display;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.actions.ActionDelegate;
030: import org.eclipse.ui.actions.WorkspaceModifyOperation;
031: import org.eclipse.ui.examples.navigator.PropertiesTreeData;
032: import org.eclipse.ui.internal.examples.navigator.Activator;
033:
034: /**
035: * A sample action that can delete a PropertiesTreeData item from a property file.
036: *
037: * @since 3.2
038: */
039: public class DeletePropertyAction extends ActionDelegate {
040:
041: private IStructuredSelection selection = StructuredSelection.EMPTY;
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
045: */
046: public void selectionChanged(IAction action, ISelection sel) {
047: if (sel instanceof IStructuredSelection)
048: selection = (IStructuredSelection) sel;
049: else
050: selection = StructuredSelection.EMPTY;
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
055: */
056: public void run(IAction action) {
057:
058: WorkspaceModifyOperation deletePropertyOperation = new WorkspaceModifyOperation() {
059: /* (non-Javadoc)
060: * @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
061: */
062: protected void execute(IProgressMonitor monitor)
063: throws CoreException {
064: // In production code, you should always externalize strings, but this is an example.
065: monitor
066: .beginTask(
067: "Deleting property from selection", 5); //$NON-NLS-1$
068: try {
069: if (selection.size() == 1) {
070:
071: Object firstElement = selection
072: .getFirstElement();
073: if (firstElement instanceof PropertiesTreeData) {
074: PropertiesTreeData data = (PropertiesTreeData) firstElement;
075:
076: IFile propertiesFile = data.getFile();
077: monitor.worked(1);
078:
079: if (propertiesFile != null
080: && propertiesFile.isAccessible()) {
081:
082: try {
083: // load the model
084: Properties properties = new Properties();
085: properties.load(propertiesFile
086: .getContents());
087: monitor.worked(1);
088:
089: // delete the property
090: properties.remove(data.getName());
091: monitor.worked(1);
092:
093: // persist the model to a temporary storage medium (byte[])
094: ByteArrayOutputStream output = new ByteArrayOutputStream();
095: properties.store(output, null);
096: monitor.worked(1);
097:
098: // set the contents of the properties file
099: propertiesFile
100: .setContents(
101: new ByteArrayInputStream(
102: output
103: .toByteArray()),
104: IResource.FORCE
105: | IResource.KEEP_HISTORY,
106: monitor);
107: monitor.worked(1);
108: } catch (IOException e) {
109: // handle error gracefully
110: Activator
111: .logError(
112: 0,
113: "Could not delete property!", e); //$NON-NLS-1$
114: MessageDialog
115: .openError(
116: Display
117: .getDefault()
118: .getActiveShell(),
119: "Error Deleting Property", //$NON-NLS-1$
120: "Could not delete property!"); //$NON-NLS-1$
121: }
122:
123: } else
124: // shouldn't happen, but handle error condition
125: MessageDialog
126: .openError(
127: Display
128: .getDefault()
129: .getActiveShell(),
130: "Error Deleting Property", //$NON-NLS-1$
131: "The properties file was not accessible!"); //$NON-NLS-1$
132:
133: } else
134: // shouldn't happen, but handle error condition
135: MessageDialog
136: .openError(Display.getDefault()
137: .getActiveShell(),
138: "Error Deleting Property", //$NON-NLS-1$
139: "The element that was selected was not of the right type."); //$NON-NLS-1$
140: } else
141: // shouldn't happen, but handle error condition
142: MessageDialog
143: .openError(Display.getDefault()
144: .getActiveShell(),
145: "Error Deleting Property", //$NON-NLS-1$
146: "An invalid number of properties were selected."); //$NON-NLS-1$
147: } finally {
148: monitor.done();
149: }
150: }
151: };
152: try {
153: PlatformUI.getWorkbench().getProgressService().run(true,
154: false, deletePropertyOperation);
155: } catch (InvocationTargetException e) {
156: // handle error gracefully
157: Activator.logError(0, "Could not delete property!", e); //$NON-NLS-1$
158: MessageDialog.openError(Display.getDefault()
159: .getActiveShell(), "Error Deleting Property", //$NON-NLS-1$
160: "Could not delete property!"); //$NON-NLS-1$
161: } catch (InterruptedException e) {
162: // handle error gracefully
163: Activator.logError(0, "Could not delete property!", e); //$NON-NLS-1$
164: MessageDialog.openError(Display.getDefault()
165: .getActiveShell(), "Error Deleting Property", //$NON-NLS-1$
166: "Could not delete property!"); //$NON-NLS-1$
167: }
168:
169: }
170: }
|