Skip to content

Commit

Permalink
add preferences page for file manager selection, bump version to 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonw committed Aug 5, 2011
1 parent e9838ba commit 6a1d1d9
Show file tree
Hide file tree
Showing 17 changed files with 1,030 additions and 160 deletions.
2 changes: 1 addition & 1 deletion META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OpenExplorer
Bundle-SymbolicName: OpenExplorer;singleton:=true
Bundle-Version: 1.4.0.qualifier
Bundle-Version: 1.5.0.qualifier
Bundle-Activator: openexplorer.Activator
Bundle-Vendor: Samson Wu
Bundle-Localization: plugin
Expand Down
3 changes: 2 additions & 1 deletion build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml,\
icons/
icons/,\
LICENSE
21 changes: 20 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,24 @@
</action>
</objectContribution>
</extension>

<extension
point="org.eclipse.ui.preferencePages">
<page
name="Open Explorer"
class="openexplorer.preferences.HomePreferencePage"
id="openexplorer.preferences.HomePreferencePage">
</page>
<page
id="openexplorer.preferences.FMPreferencePage"
class="openexplorer.preferences.FMPreferencePage"
name="File Manager"
category="openexplorer.preferences.HomePreferencePage">
</page>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
<initializer
class="openexplorer.preferences.PreferenceInitializer">
</initializer>
</extension>
</plugin>
3 changes: 3 additions & 0 deletions src/openexplorer/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "OpenExplorer"; //$NON-NLS-1$

// The plugin version
public static final String VERSION = "1.5.0";

// The shared instance
private static Activator plugin;

Expand Down
217 changes: 91 additions & 126 deletions src/openexplorer/actions/AbstractOpenExplorerAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@

import java.io.IOException;

import openexplorer.util.IOUtils;
import openexplorer.Activator;
import openexplorer.util.Messages;
import openexplorer.util.OperatingSystem;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
Expand All @@ -47,141 +51,102 @@
* @author <a href="mailto:samson959@gmail.com">Samson Wu</a>
* @version 1.4.0
*/
public abstract class AbstractOpenExplorerAction implements IActionDelegate {
protected IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
protected Shell shell;
protected ISelection currentSelection;
public abstract class AbstractOpenExplorerAction implements IActionDelegate,
IPropertyChangeListener {
protected IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
protected Shell shell;
protected ISelection currentSelection;

protected String os;
public static final String WINDOWS = "win32";
public static final String LINUX = "linux";
public static final String MACOSX = "macosx";
protected String systemBrowser;

protected String systemBrowser = "explorer";

public AbstractOpenExplorerAction() {
this.os = System.getProperty("osgi.os");
if (WINDOWS.equalsIgnoreCase(this.os)) {
this.systemBrowser = "explorer";
} else if (LINUX.equalsIgnoreCase(this.os)) {
this.systemBrowser = detachLinuxBrowser();
} else if (MACOSX.equalsIgnoreCase(this.os)) {
this.systemBrowser = "open";
}
}

/**
* Use {@code which} command to found the modern file manager, if not found use the xdg-open.
* @return the file manager
*/
protected String detachLinuxBrowser() {
String result = executeCommand("which dolphin");
if (result == null || result.trim().equals("")) {
result = executeCommand("which nautilus");
}
if (result == null || result.trim().equals("")) {
result = executeCommand("which thunar");
}
if (result == null || result.trim().equals("")) {
result = executeCommand("which pcmanfm");
}
if (result == null || result.trim().equals("")) {
result = executeCommand("which rox");
}
if (result == null || result.trim().equals("")) {
result = "xdg-open";
}
return result;
}
public AbstractOpenExplorerAction() {
this.systemBrowser = OperatingSystem.INSTANCE.getSystemBrowser();
Activator.getDefault().getPreferenceStore()
.addPropertyChangeListener(this);
}

/**
* execute the command and return the result.
* @param command
* @return
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
* .jface.util.PropertyChangeEvent)
*/
private String executeCommand(String command) {
String stdout = null;
try {
Process process = Runtime.getRuntime().exec(command);
stdout = IOUtils.toString(process.getInputStream());
stdout = stdout.trim();
stdout = stdout.replace("\n", "");
stdout = stdout.replace("\r", "");
} catch (IOException e) {
e.printStackTrace();
public void propertyChange(PropertyChangeEvent event) {
if (OperatingSystem.INSTANCE.isLinux()) {
this.systemBrowser = OperatingSystem.INSTANCE.getSystemBrowser();
}
return stdout;
}

public void run(IAction action) {
if (this.currentSelection == null || this.currentSelection.isEmpty()) {
return;
}
if (this.currentSelection instanceof ITreeSelection) {
ITreeSelection treeSelection = (ITreeSelection) this.currentSelection;
if (this.currentSelection == null || this.currentSelection.isEmpty()) {
return;
}
if (this.currentSelection instanceof ITreeSelection) {
ITreeSelection treeSelection = (ITreeSelection) this.currentSelection;

TreePath[] paths = treeSelection.getPaths();
TreePath[] paths = treeSelection.getPaths();

for (int i = 0; i < paths.length; i++) {
TreePath path = paths[i];
IResource resource = null;
Object segment = path.getLastSegment();
if ((segment instanceof IResource))
resource = (IResource) segment;
else if ((segment instanceof IJavaElement)) {
resource = ((IJavaElement) segment).getResource();
}
if (resource == null) {
continue;
}
String browser = this.systemBrowser;
String location = resource.getLocation().toOSString();
if ((resource instanceof IFile)) {
location = ((IFile) resource).getParent().getLocation()
.toOSString();
if (WINDOWS.equalsIgnoreCase(this.os)) {
browser = this.systemBrowser + " /select,";
location = ((IFile) resource).getLocation()
.toOSString();
}
}
openInBrowser(browser, location);
}
} else if (this.currentSelection instanceof ITextSelection
|| this.currentSelection instanceof IStructuredSelection) {
// open current editing file
IEditorPart editor = window.getActivePage().getActiveEditor();
if (editor != null) {
IFile current_editing_file = (IFile) editor.getEditorInput()
.getAdapter(IFile.class);
String browser = this.systemBrowser;
String location = current_editing_file.getParent()
.getLocation().toOSString();
if (WINDOWS.equalsIgnoreCase(this.os)) {
browser = this.systemBrowser + " /select,";
location = current_editing_file.getLocation().toOSString();
}
openInBrowser(browser, location);
}
}
}
for (int i = 0; i < paths.length; i++) {
TreePath path = paths[i];
IResource resource = null;
Object segment = path.getLastSegment();
if ((segment instanceof IResource))
resource = (IResource) segment;
else if ((segment instanceof IJavaElement)) {
resource = ((IJavaElement) segment).getResource();
}
if (resource == null) {
continue;
}
String browser = this.systemBrowser;
String location = resource.getLocation().toOSString();
if ((resource instanceof IFile)) {
location = ((IFile) resource).getParent().getLocation()
.toOSString();
if (OperatingSystem.INSTANCE.isWindows()) {
browser = this.systemBrowser + " /select,";
location = ((IFile) resource).getLocation()
.toOSString();
}
}
openInBrowser(browser, location);
}
} else if (this.currentSelection instanceof ITextSelection
|| this.currentSelection instanceof IStructuredSelection) {
// open current editing file
IEditorPart editor = window.getActivePage().getActiveEditor();
if (editor != null) {
IFile current_editing_file = (IFile) editor.getEditorInput()
.getAdapter(IFile.class);
String browser = this.systemBrowser;
String location = current_editing_file.getParent()
.getLocation().toOSString();
if (OperatingSystem.INSTANCE.isWindows()) {
browser = this.systemBrowser + " /select,";
location = current_editing_file.getLocation().toOSString();
}
openInBrowser(browser, location);
}
}
}

protected void openInBrowser(String browser, String location) {
try {
if (WINDOWS.equalsIgnoreCase(this.os)) {
Runtime.getRuntime().exec(browser + " \"" + location + "\"");
} else {
Runtime.getRuntime().exec(new String[] { browser, location });
}
} catch (IOException e) {
MessageDialog.openError(shell, "OpenExploer Error", "Can't open \""
+ location + "\"");
e.printStackTrace();
}
}
protected void openInBrowser(String browser, String location) {
try {
if (OperatingSystem.INSTANCE.isWindows()) {
Runtime.getRuntime().exec(browser + " \"" + location + "\"");
} else {
Runtime.getRuntime().exec(new String[] { browser, location });
}
} catch (IOException e) {
MessageDialog.openError(shell, Messages.OpenExploer_Error,
Messages.Cant_Open + " \"" + location + "\"");
e.printStackTrace();
}
}

public void selectionChanged(IAction action, ISelection selection) {
this.currentSelection = selection;
}
public void selectionChanged(IAction action, ISelection selection) {
this.currentSelection = selection;
}
}
Loading

0 comments on commit 6a1d1d9

Please sign in to comment.