Java Code Examples for org.openide.util.Exceptions#findLocalizedMessage()

The following examples show how to use org.openide.util.Exceptions#findLocalizedMessage() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File: FileObjTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testReadOnlyFile() throws Exception {
    clearWorkDir();
    File f = new File(getWorkDir(), "read-only.txt");
    f.createNewFile();
    FileObject fo = FileUtil.toFileObject(f);
    f.setWritable(false);
    try {
        OutputStream os = fo.getOutputStream();
        fail("Can't get the output stream for read-only file: " + os);
    } catch (IOException ex) {
        String msg = Exceptions.findLocalizedMessage(ex);
        assertNotNull("The exception comes with a localized message", msg);
    } finally {
        f.setWritable(true);
    }
}
 
Example 2
Source Project: netbeans   File: ExitDialog.java    License: Apache License 2.0 6 votes vote down vote up
/** Tries to save given data object using its save cookie.
 * Notifies user if excetions appear.
 */
private void save (Savable sc) {
    try {
        if (sc != null) {
            sc.save();
        }
        // only remove the object if the save succeeded
        listModel.removeElement(sc);
    } catch (java.io.IOException exc) {
        Throwable t = exc;
        if (Exceptions.findLocalizedMessage(exc) == null) {
            t = Exceptions.attachLocalizedMessage(exc,
                                              NbBundle.getBundle(ExitDialog.class).getString("EXC_Save"));
        }
        Exceptions.printStackTrace(t);
    }
}
 
Example 3
Source Project: netbeans   File: ExceptionsTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testAttachLocalizedMessageForWeirdException() {
    class WeirdEx extends Exception {
        public WeirdEx(String message) {
            super(message);
        }

        @Override
        public Throwable getCause() {
            return null;
        }
    }

    Exception e = new WeirdEx("Help");
    String msg = "me please";

    Exception expResult = e;
    Exception result = Exceptions.attachMessage(e, msg);
    assertEquals(expResult, result);

    String fnd = Exceptions.findLocalizedMessage(e);

    assertEquals("No localized msg found", null, fnd);

    assertCleanStackTrace(e);
}
 
Example 4
public void testDoNotLoadInvalidProject() throws Exception {
    String content = projdir.getFileObject("nbproject/project.xml").asText("UTF-8");
    TestFileUtils.writeFile(projdir, "nbproject/project.xml", content.replace("</project>", "<bogus/>\n</project>"));
    try {
        ProjectManager.getDefault().findProject(projdir);
        fail("should not have successfully loaded an invalid project.xml");
    } catch (IOException x) {
        assertTrue(x.toString(), x.getMessage().contains("bogus"));
        // #142079: use simplified error message.
        String loc = Exceptions.findLocalizedMessage(x);
        assertNotNull(loc);
        assertTrue(loc, loc.contains("bogus"));
        assertTrue(loc, loc.contains("project.xml"));
        // Probably should not assert exact string, as this is dependent on parser.
    }
}
 
Example 5
Source Project: netbeans   File: ViewConfigAction.java    License: Apache License 2.0 6 votes vote down vote up
@Messages({"# {0} - URL", "ViewConfigAction_could_not_connect=Could not retrieve: {0}"})
@Override public InputStream inputStream() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        URLConnection conn = new ConnectionBuilder().homeURL(new URL(home)).url(url).connection();
        InputStream is = conn.getInputStream();
        try {
            FileUtil.copy(is, baos);
        } finally {
            is.close();
        }
    } catch (IOException x) {
        if (Exceptions.findLocalizedMessage(x) == null) {
            Exceptions.attachLocalizedMessage(x, ViewConfigAction_could_not_connect(url));
        }
        throw x;
    }
    return new ByteArrayInputStream(baos.toByteArray());
}
 
Example 6
Source Project: netbeans   File: ViewUtil.java    License: Apache License 2.0 5 votes vote down vote up
static void nodeRename(final Node n, final String newStr) {
    // bugfix #21589 don't update name if there is not any change
    if (n.getName().equals(newStr)) {
        return;
    }
    if (EventQueue.isDispatchThread() && Boolean.TRUE.equals(n.getValue("slowRename"))) { // NOI18N
        RP.post(new Runnable() {
            @Override
            public void run() {
                nodeRename(n, newStr);
            }
        });
        return;
    }
    try {
        n.setName(newStr);
    } catch (IllegalArgumentException exc) {
        boolean needToAnnotate = Exceptions.findLocalizedMessage(exc) == null;

        // annotate new localized message only if there is no localized message yet
        if (needToAnnotate) {
            String msg = NbBundle.getMessage(
                    TreeViewCellEditor.class, "RenameFailed", n.getName(), newStr
                );
            Exceptions.attachLocalizedMessage(exc, msg);
        }

        Exceptions.printStackTrace(exc);
    }
}
 
Example 7
Source Project: netbeans   File: PropertyDialogManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tries to find an annotation with localized message(primarily) or general
 * message in all exception annotations. If the annotation with the message
 * is found it is notified with original severity. If the message is not
 * found the exception is notified with informational severity.
 *
 * @param e exception to notify
 */
private static void notifyUser(Exception e) {
    String userMessage = Exceptions.findLocalizedMessage(e);

    if (userMessage != null) {
        Exceptions.printStackTrace(e);
    } else {
        // if there is not user message don't bother user, just log an
        // exception
        Logger.getLogger(PropertyDialogManager.class.getName()).log(Level.INFO, null, e);
    }
}
 
Example 8
Source Project: netbeans   File: PropertyEnv.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * A setter that should be used by the property editor
 * to change the state of the environment.
 * Even the state property is bound, changes made from the editor itself
 * are allowed without restrictions.
 */
public void setState(Object newState) {
    if (getState().equals(newState)) {
        // no change, no fire vetoable and property change
        return;
    }

    try {
        getSupport().fireVetoableChange(PROP_STATE, getState(), newState);
        state = newState;

        // always notify state change
        getChange().firePropertyChange(PROP_STATE, null, newState);
    } catch (PropertyVetoException pve) {
        // and notify the user that the change cannot happen
        String msg = Exceptions.findLocalizedMessage(pve);
        if (msg == null) {
            msg = pve.getLocalizedMessage();
        }
        if (msg != null) {
            DialogDisplayer.getDefault().notify(
                    new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE));
        } else { // no message for the user, log the exception the standard way
            PropertyDialogManager.notify(pve);
        }
    }
}
 
Example 9
Source Project: netbeans   File: ExceptionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testAttachLocalizedMessage() {
    Exception e = new Exception("Help");
    String msg = "me please";
    
    Exception expResult = e;
    Exception result = Exceptions.attachLocalizedMessage(e, msg);
    assertEquals(expResult, result);

    String fnd = Exceptions.findLocalizedMessage(e);

    assertEquals("The same msg", msg, fnd);

    assertCleanStackTrace(e);
}
 
Example 10
Source Project: netbeans   File: ExceptionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testAttachLocalizedMessageForClassNFE() {
    Exception e = new ClassNotFoundException("Help");
    String msg = "me please";
    
    Exception expResult = e;
    Exception result = Exceptions.attachLocalizedMessage(e, msg);
    assertEquals(expResult, result);

    String fnd = Exceptions.findLocalizedMessage(e);

    assertEquals("The same msg", msg, fnd);

    assertCleanStackTrace(e);
}
 
Example 11
Source Project: netbeans   File: ExceptionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testAttachLocalizedMessageForClassNFEIfNoMsg() {
    Exception e = new ClassNotFoundException("Help");
    String msg = "me please";
    
    Exception expResult = e;
    Exception result = Exceptions.attachMessage(e, msg);
    assertEquals(expResult, result);

    String fnd = Exceptions.findLocalizedMessage(e);

    assertEquals("No localized msg found", null, fnd);

    assertCleanStackTrace(e);
}
 
Example 12
Source Project: netbeans   File: Page.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setName(String s) {

    String oldDisplayName = getDisplayName();
    try {
        if (!pc.isPageInAnyFacesConfig(oldDisplayName)) {
            original.setName(s);
        } else {
            renaming = true;
            original.setName(s);
            String newDisplayName = original.getDisplayName();
            if (isDataNode()) {
                newDisplayName = getFolderDisplayName(pc.getWebFolder(), ((DataNode) original).getDataObject().getPrimaryFile());
            }
            pc.saveLocation(oldDisplayName, newDisplayName);
            renaming = false;
            pc.renamePageInModel(oldDisplayName, newDisplayName);
        }
    } catch (IllegalArgumentException iae) {

        // determine if "printStackTrace"  and  "new annotation" of this exception is needed
        boolean needToAnnotate = Exceptions.findLocalizedMessage(iae) == null;

        // annotate new localized message only if there is no localized message yet
        if (needToAnnotate) {
            Exceptions.attachLocalizedMessage(iae, NbBundle.getMessage(Page.class, "MSG_BadFormat", oldDisplayName, s));
        }

        Exceptions.printStackTrace(iae);
    }
}
 
Example 13
Source Project: netbeans   File: CloneableEditorSupport.java    License: Apache License 2.0 4 votes vote down vote up
/** Called when the document is being modified.
* The responsibility of this method is to inform the environment
* that its document is modified. Current implementation
* Just calls env.setModified (true) to notify it about
* modification.
*
* @return true if the environment accepted being marked as modified
*    or false if it refused it and the document should still be unmodified
*/
protected boolean notifyModified() {
    boolean locked = true;

    try {
        env.markModified();
        // #239622 - since notifyModified() could be called directly (in case of extending CES)
        // check that alreadyModified flag is also set. Otherwise callNotifyUnmodified()
        // would not proceed to notifyUnmodified() call (it would end up on checking alreadyModified flag).
        synchronized (checkModificationLock) {
            if (!isAlreadyModified()) {
                setAlreadyModified(true);
            }
        }

    } catch (final UserQuestionException ex) {
        synchronized (this) {
            if (!this.inUserQuestionExceptionHandler) {
                this.inUserQuestionExceptionHandler = true;
                DocumentOpenClose.RP.post(new Runnable() {
                   public void run() {
                       NotifyDescriptor nd = new NotifyDescriptor.Confirmation(ex.getLocalizedMessage(),
                                                                               NotifyDescriptor.YES_NO_OPTION);
                       Object res = DialogDisplayer.getDefault().notify(nd);

                       if (NotifyDescriptor.OK_OPTION.equals(res)) {
                           try {
                               ex.confirmed();
                           }
                           catch (IOException ex1) {
                               Exceptions.printStackTrace(ex1);
                           }
                       }
                       synchronized (CloneableEditorSupport.this) {
                           CloneableEditorSupport.this.inUserQuestionExceptionHandler = false;
                       }
                   }
               });
            }
        }
        
        locked = false;
        ERR.log(Level.FINE, "Could not lock document", ex);
    } catch (IOException e) { // locking failed
        //#169695: Added exception log to investigate
        ERR.log(Level.FINE, "Could not lock document", e);
        //#169695: END
        String message = null;

        if ((Object)e.getMessage() != e.getLocalizedMessage()) {
            message = e.getLocalizedMessage();
        } else {
            message = Exceptions.findLocalizedMessage(e);
        }

        if (message != null) {
            StatusDisplayer.getDefault().setStatusText(message);
        }

        locked = false;
    }

    if (!locked) {
        Toolkit.getDefaultToolkit().beep();
        ERR.log(Level.FINE, "notifyModified returns false");
        return false;
    }

    // source modified, remove it from tab-reusing slot
    lastReusable.clear();
    updateTitles();

    if (ERR.isLoggable(Level.FINE)) {
        ERR.log(Level.FINE, "notifyModified returns true; env.isModified()=" + env.isModified());
    }
    return true;
}
 
Example 14
Source Project: netbeans   File: RenameAction.java    License: Apache License 2.0 4 votes vote down vote up
protected void performAction(final Node[] activatedNodes) {
    if (activatedNodes == null || activatedNodes.length == 0) {
        return;
    }
    Node n = activatedNodes[0]; // we supposed that one node is activated
    
    // for slow FS perform rename out of EDT
    if (EventQueue.isDispatchThread() && Boolean.TRUE.equals(n.getValue("slowRename"))) { // NOI18N
        RP.post(new Runnable() {
            @Override
            public void run() {
                performAction(activatedNodes);
            }
        });
        return;
    }

    NotifyDescriptor.InputLine dlg = new NotifyDescriptor.InputLine(
            NbBundle.getMessage(RenameAction.class, "CTL_RenameLabel"),
            NbBundle.getMessage(RenameAction.class, "CTL_RenameTitle")
        );
    dlg.setInputText(n.getName());

    if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(dlg))) {
        String newname = null;

        try {
            newname = dlg.getInputText();

            if (!newname.equals("")) {
                n.setName(dlg.getInputText()); // NOI18N
            }
        } catch (IllegalArgumentException e) {
            // determine if "printStackTrace"  and  "new annotation" of this exception is needed
            boolean needToAnnotate = Exceptions.findLocalizedMessage(e) == null;

            // annotate new localized message only if there is no localized message yet
            if (needToAnnotate) {
                Exceptions.attachLocalizedMessage(e,
                                                  NbBundle.getMessage(RenameAction.class,
                                                                      "MSG_BadFormat",
                                                                      n.getName(),
                                                                      newname));
            }

            Exceptions.printStackTrace(e);
        }
    }
}