/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.actions;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import de.innovationgate.eclipse.utils.wga.SortedProperties;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
public class SortLabelFile implements IActionDelegate {
Properties _props;
private IFile _file;
public void run(IAction action) {
MessageBox msgBox = new MessageBox(new Shell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
msgBox.setText("Are you really sure?");
msgBox.setMessage("Are you really sure that you want to sort the labelfile: \n" + _file.getFullPath());
int response = msgBox.open();
if (response == SWT.YES) {
SortedProperties sortedProperties = new SortedProperties();
sortedProperties.putAll(_props);
FileOutputStream labelFileStream = null;
try {
labelFileStream = new FileOutputStream(_file.getLocation().toString());
sortedProperties.store(labelFileStream, null);
_file.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
} catch (IOException e) {
WGADesignerPlugin.getDefault().logInfo("Unable to store file: " + _file.getLocation(), e);
} catch (CoreException e) {
WGADesignerPlugin.getDefault().logInfo("Unable to refresh resource on: " + _file.getLocation(), e);
} finally {
try {
labelFileStream.close();
} catch (IOException e) {
WGADesignerPlugin.getDefault().logInfo("Unable to close FileOutputStream for: " + _file.getLocation(), e);
}
}
}
}
public void selectionChanged(IAction action, ISelection selection) {
action.setEnabled(false);
if (selection != null && selection instanceof IStructuredSelection) {
IStructuredSelection structSelection = (IStructuredSelection) selection;
Object selectedElement = structSelection.getFirstElement();
if (selectedElement instanceof IFile) {
_file = (IFile) selectedElement;
if (_file.getFileExtension().equals("properties")) {
IContainer container = _file.getParent();
if (container.getName().startsWith("labels_")) {
Properties props = WGADesignStructureHelper.loadLabel(_file);
if (!props.isEmpty()) {
_props = props;
action.setEnabled(true);
}
}
}
}
}
}
}