Skip to content

Commit

Permalink
fixed URI parsing, fixed file and folder opening
Browse files Browse the repository at this point in the history
  • Loading branch information
raydac committed Oct 3, 2015
1 parent 185e2e8 commit 8e38a55
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public MMapURI(final URI uri) {
this.parameters = ModelUtils.extractQueryPropertiesFromURI(uri);
if (this.fileUriFlag) {
try {
preparedURI = new URI(uri.getScheme(), null, extractHost(uri), -1, uri.getPath(), null, null);
final String uriAsString = uri.toString();
final int queryStart = uriAsString.lastIndexOf('?');
preparedURI = new URI(queryStart >= 0 ? uriAsString.substring(0,queryStart) : uriAsString);
}
catch (URISyntaxException ex) {
throw new Error("Unexpected error", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testCreate_Str() throws Exception {
}

@Test
public void testCreate_Linux_Uri() throws Exception {
public void testCreate_Linux_Uri_NoProps() throws Exception {
if (SystemUtils.IS_OS_LINUX) {
MMapURI uri = new MMapURI(new URI("file:///K%C3%B5ik/v%C3%B5i/mitte/midagi.txt"));
assertEquals("file:///K%C3%B5ik/v%C3%B5i/mitte/midagi.txt", uri.asString(false, false));
Expand All @@ -95,7 +95,29 @@ public void testCreate_Linux_Uri() throws Exception {
}

@Test
public void testCreate_Windows_Uri() throws Exception {
public void testCreate_Linux_Uri_Props() throws Exception {
if (SystemUtils.IS_OS_LINUX) {
MMapURI uri = new MMapURI(new URI("file:///K%C3%B5ik/v%C3%B5i/mitte/midagi.txt?one=two"));
assertEquals("two",uri.getParameters().getProperty("one"));
assertEquals("file:///K%C3%B5ik/v%C3%B5i/mitte/midagi.txt?one=two", uri.asString(false, true));
assertEquals(new File("/Kõik/või/mitte/midagi.txt"), uri.asFile(null));
assertTrue(uri.isAbsolute());
assertFalse(uri.getParameters().isEmpty());

uri = new MMapURI(new URI("/some/test"));
assertEquals("/some/test", uri.asString(false, false));
assertFalse(uri.isAbsolute());
assertTrue(uri.getParameters().isEmpty());

uri = new MMapURI(new URI("/some/test?hello=1234"));
assertEquals("/some/test?hello=1234", uri.asString(false, true));
assertFalse(uri.isAbsolute());
assertEquals(1, uri.getParameters().size());
}
}

@Test
public void testCreate_Windows_Uri_NoProps() throws Exception {
if (SystemUtils.IS_OS_WINDOWS) {
MMapURI uri = new MMapURI(new URI("file://C:/K%C3%B5ik/v%C3%B5i/mitte/midagi.txt"));
assertEquals("file://C:/K%C3%B5ik/v%C3%B5i/mitte/midagi.txt", uri.asString(false, false));
Expand All @@ -115,6 +137,28 @@ public void testCreate_Windows_Uri() throws Exception {
}
}

@Test
public void testCreate_Windows_Uri_Props() throws Exception {
if (SystemUtils.IS_OS_WINDOWS) {
MMapURI uri = new MMapURI(new URI("file://C:/K%C3%B5ik/v%C3%B5i/mitte/midagi.txt?one=two"));
assertEquals("file://C:/K%C3%B5ik/v%C3%B5i/mitte/midagi.txt?one=two", uri.asString(false, true));
assertEquals(new File("C:\\Kõik\\või\\mitte\\midagi.txt"), uri.asFile(null));
assertTrue(uri.isAbsolute());
assertFalse(uri.getParameters().isEmpty());
assertEquals("two",uri.getParameters().getProperty("one"));

uri = new MMapURI(new URI("/some/test"));
assertEquals("/some/test", uri.asString(false, false));
assertFalse(uri.isAbsolute());
assertTrue(uri.getParameters().isEmpty());

uri = new MMapURI(new URI("/some/test?hello=1234"));
assertEquals("/some/test?hello=1234", uri.asString(false, true));
assertFalse(uri.isAbsolute());
assertEquals(1, uri.getParameters().size());
}
}

@Test
public void testCreate_AbsFile_Linux_NoBase_NoProps() throws Exception {
if (SystemUtils.IS_OS_LINUX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public void onClickOnExtra(final MindMapPanel source, final int clicks, final To
if (fileObj == null) {
logger.warn("Can't find FileObject for " + theFile);
if (theFile.exists()) {
NbUtils.openInExternalEditor(theFile);
NbUtils.openInSystemViewer(theFile);
}
else {
NbUtils.msgError(String.format(BUNDLE.getString("MMDGraphEditor.onClickExtra.errorCanfFindFile"), theFile.getAbsolutePath()));
Expand All @@ -396,11 +396,13 @@ public void onClickOnExtra(final MindMapPanel source, final int clicks, final To

try {
if (Boolean.parseBoolean(uri.getParameters().getProperty(FILELINK_ATTR_OPEN_IN_SYSTEM, "false"))) { //NOI18N
NbUtils.openInExternalEditor(theFile);
NbUtils.openInSystemViewer(theFile);
}
else {
Project projectOwner = null;
if (fileObj.isFolder()) {
if (FileOwnerQuery.getOwner(fileObj) != null) {
projectOwner = FileOwnerQuery.getOwner(fileObj);
if (projectOwner != null) {
final DataObject dataObj = DataObject.find(fileObj);
if (dataObj instanceof DataFolder) {
final ProjectManager manager = ProjectManager.getDefault();
Expand All @@ -420,23 +422,29 @@ public void onClickOnExtra(final MindMapPanel source, final int clicks, final To
}

final ProjectManager manager = ProjectManager.getDefault();
final OpenProjects openManager = OpenProjects.getDefault();
if (manager.isProject(fileObj)) {
final Project project = manager.findProject(fileObj);
if (project == null) {
if (!NbUtils.SelectIn.FAVORITES.select(this, fileObj)) {
NbUtils.openInExternalEditor(theFile);
NbUtils.openInSystemViewer(theFile);
}
}
else {
final OpenProjects openManager = OpenProjects.getDefault();
if (!openManager.isProjectOpen(project)) {
openManager.open(new Project[]{project}, false, true);
}
}
}
else {
if (projectOwner!=null){
openManager.open(new Project[]{projectOwner}, false);
if (!NbUtils.SelectIn.PROJECTS.select(this, fileObj)) {
NbUtils.openInSystemViewer(theFile);
}
}else
if (!NbUtils.SelectIn.FAVORITES.select(this, fileObj)) {
NbUtils.openInExternalEditor(theFile);
NbUtils.openInSystemViewer(theFile);
}
}
}
Expand All @@ -446,6 +454,7 @@ public void onClickOnExtra(final MindMapPanel source, final int clicks, final To
if (openable != null) {
openable.open();
}
NbUtils.SelectIn.PROJECTS.select(this, fileObj);
}
}
}
Expand Down Expand Up @@ -598,7 +607,7 @@ else if (Project.class.isAssignableFrom(representation)) {
}
}

DataObject dataObject = null;
DataObject dataObject;

if (nodeObjectFlavor != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
private void labelBrowseCurrentLinkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelBrowseCurrentLinkMouseClicked
if (evt.getClickCount() > 1) {
final File file = new File(this.textFieldFilePath.getText().trim());
NbUtils.openInExternalEditor(file);
NbUtils.openInSystemViewer(file);
}
}//GEN-LAST:event_labelBrowseCurrentLinkMouseClicked

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,44 +343,42 @@ public static boolean browseURI(final URI uri, final boolean preferInsideBrowser
}
}

public static void openInExternalEditor(final File file) {
public static void openInSystemViewer(final File file) {
final Runnable startEdit = new Runnable() {
@Override
public void run() {
boolean ok = false;
if (Desktop.isDesktopSupported()) {
final Desktop dsk = Desktop.getDesktop();
if (dsk.isSupported(Desktop.Action.EDIT)) {
try {
dsk.edit(file);
ok = true;
}
catch (Throwable ex) {
logger.error("Can't start file edit: " + file, ex);
}
}
if (!ok && dsk.isSupported(Desktop.Action.OPEN)) {
if (dsk.isSupported(Desktop.Action.OPEN)) {
try {
dsk.open(file);
ok = true;
}
catch (Throwable ex) {
logger.error("Can't start file open: " + file, ex);
logger.error("Can't open file in system viewer : " + file, ex);//NOI18N
}
}
}
if (!ok) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
NbUtils.msgError("Can't open file to edit! See the log!");
NbUtils.msgError("Can't open file in system viewer! See the log!");//NOI18N
Toolkit.getDefaultToolkit().beep();
}
});
}
}
};
final Thread thr = new Thread(startEdit, " MMDStartFileEdit");
final Thread thr = new Thread(startEdit, " MMDStartFileEdit");//NOI18N
thr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread t, final Throwable e) {
logger.error("Detected uncaught exception in openInSystemViewer() for file " + file, e);
}
});

thr.setDaemon(true);
thr.start();
}
Expand Down

0 comments on commit 8e38a55

Please sign in to comment.