Skip to content

Commit

Permalink
Merge pull request #1 from yananzhi/dos
Browse files Browse the repository at this point in the history
Remove the dependency to JDT, so we can use it in CDT, Add dos here a…
  • Loading branch information
yananzhi committed Nov 25, 2015
2 parents ecde7ae + 7234b98 commit c42441d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 23 deletions.
1 change: 0 additions & 1 deletion META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Bundle-Localization: plugin
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.core.resources,
org.eclipse.jdt.core,
org.eclipse.jface.text
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-ActivationPolicy: lazy
Expand Down
8 changes: 7 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@
objectClass="org.eclipse.core.runtime.IAdaptable"
id="OpenExplorer.popupMenuContribution">
<action
label="Open Explorer"
label="Open Explorer(&amp;Q)"
icon="icons/OpenExplorer.png"
class="openexplorer.actions.OpenExplorerPopupAction"
menubarPath="additions"
enablesFor="*"
id="OpenExplorer.popupMenuAction">
</action>
<action
class="openexplorer.actions.DosHere"
id="OpenExplorer.action1"
label="DosHere(&amp;D)"
menubarPath="additions">
</action>
</objectContribution>
</extension>
<extension
Expand Down
32 changes: 11 additions & 21 deletions src/openexplorer/actions/AbstractOpenExplorerAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

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;
Expand All @@ -51,19 +50,16 @@
* @author <a href="mailto:samson959@gmail.com">Samson Wu</a>
* @version 1.4.0
*/
public abstract class AbstractOpenExplorerAction implements IActionDelegate,
IPropertyChangeListener {
protected IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
public abstract class AbstractOpenExplorerAction implements IActionDelegate, IPropertyChangeListener {
protected IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
protected Shell shell;
protected ISelection currentSelection;

protected String systemBrowser;

public AbstractOpenExplorerAction() {
this.systemBrowser = OperatingSystem.INSTANCE.getSystemBrowser();
Activator.getDefault().getPreferenceStore()
.addPropertyChangeListener(this);
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(this);
}

/*
Expand All @@ -80,6 +76,7 @@ public void propertyChange(PropertyChangeEvent event) {
}

public void run(IAction action) {

if (this.currentSelection == null || this.currentSelection.isEmpty()) {
return;
}
Expand All @@ -94,35 +91,29 @@ public void run(IAction action) {
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();
location = ((IFile) resource).getParent().getLocation().toOSString();
if (OperatingSystem.INSTANCE.isWindows()) {
browser = this.systemBrowser + " /select,";
location = ((IFile) resource).getLocation()
.toOSString();
location = ((IFile) resource).getLocation().toOSString();
}
}
openInBrowser(browser, location);
}
} else if (this.currentSelection instanceof ITextSelection
|| this.currentSelection instanceof IStructuredSelection) {
|| 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);
IFile current_editing_file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
String browser = this.systemBrowser;
String location = current_editing_file.getParent()
.getLocation().toOSString();
String location = current_editing_file.getParent().getLocation().toOSString();
if (OperatingSystem.INSTANCE.isWindows()) {
browser = this.systemBrowser + " /select,";
location = current_editing_file.getLocation().toOSString();
Expand All @@ -140,8 +131,7 @@ protected void openInBrowser(String browser, String location) {
Runtime.getRuntime().exec(new String[] { browser, location });
}
} catch (IOException e) {
MessageDialog.openError(shell, Messages.OpenExploer_Error,
Messages.Cant_Open + " \"" + location + "\"");
MessageDialog.openError(shell, Messages.OpenExploer_Error, Messages.Cant_Open + " \"" + location + "\"");
e.printStackTrace();
}
}
Expand Down
115 changes: 115 additions & 0 deletions src/openexplorer/actions/DosHere.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package openexplorer.actions;
/**
* Copyright (c) 2011 zhiyanan
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import openexplorer.util.Messages;
import openexplorer.util.OperatingSystem;

public class DosHere implements IActionDelegate {

protected ISelection currentSelection;
protected IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

public DosHere() {
// TODO Auto-generated constructor stub
}


public void run(IAction action) {


if (this.currentSelection == null || this.currentSelection.isEmpty()) {
return;
}
if (this.currentSelection instanceof ITreeSelection) {
ITreeSelection treeSelection = (ITreeSelection) this.currentSelection;

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;

if (resource == null) {
continue;
}
String location = resource.getLocation().toOSString();
if ((resource instanceof IFile)) {
location = ((IFile) resource).getParent().getLocation().toOSString();
}
openInTerminal( location);
return ;
}
} else if (this.currentSelection instanceof ITextSelection
|| this.currentSelection instanceof IStructuredSelection) {
IEditorPart editor = window.getActivePage().getActiveEditor();
if (editor != null) {
IFile current_editing_file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
String location = current_editing_file.getParent().getLocation().toOSString();

openInTerminal( location);
return ;
}
}

MessageDialog.openError(window.getShell(), Messages.OpenExploer_Error, Messages.Cant_Open + " Unsupport selection:" + this.currentSelection);

}

protected void openInTerminal(String location) {
try {
if (OperatingSystem.INSTANCE.isWindows()) {
Runtime.getRuntime().exec("cmd /c start cmd /k cd /d " + " \"" + location + "\"");
} else {
MessageDialog.openError(window.getShell(), Messages.OpenExploer_Error, Messages.Cant_Open + " Only support windows now");
}
} catch (IOException e) {
MessageDialog.openError(window.getShell(), Messages.OpenExploer_Error, Messages.Cant_Open + " \"" + location + "\"");
e.printStackTrace();
}
}

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

}

}

0 comments on commit c42441d

Please sign in to comment.