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

The following are Jave code examples for showing how to use findLocalizedMessage() of the org.openide.util.Exceptions class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: FileObjTest.java   View Source Code Vote up 6 votes
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
Project: incubator-netbeans   File: ExitDialog.java   View Source Code Vote up 6 votes
/** 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
Project: incubator-netbeans   File: AntBasedProjectFactorySingletonTest.java   View Source Code Vote up 6 votes
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 4
Project: incubator-netbeans   File: ExceptionsTest.java   View Source Code Vote up 6 votes
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 5
Project: incubator-netbeans   File: ViewConfigAction.java   View Source Code Vote up 6 votes
@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
Project: incubator-netbeans   File: ViewUtil.java   View Source Code Vote up 5 votes
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
Project: incubator-netbeans   File: PropertyDialogManager.java   View Source Code Vote up 5 votes
/**
 * 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
Project: incubator-netbeans   File: PropertyEnv.java   View Source Code Vote up 5 votes
/**
 * 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
Project: incubator-netbeans   File: ExceptionsTest.java   View Source Code Vote up 5 votes
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
Project: incubator-netbeans   File: ExceptionsTest.java   View Source Code Vote up 5 votes
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
Project: incubator-netbeans   File: ExceptionsTest.java   View Source Code Vote up 5 votes
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
Project: incubator-netbeans   File: RenameAction.java   View Source Code Vote up 4 votes
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);
        }
    }
}