Beispiel #1
0
  /**
   * Exports the current view as a SVG image after prompting the user for a filename.
   *
   * @param parent Parent frame that is used to display error messages.
   * @param graph Graph to be exported to a SVG file.
   */
  public static void exportAsSvg(final JFrame parent, final ZyGraph graph) {
    Preconditions.checkNotNull(parent, "IE01737: Parent argument can not be null");
    Preconditions.checkNotNull(graph, "IE01738: Graph argument can not be null");

    final LastDirFileChooser fileChooser = new LastDirFileChooser();
    final int retval = fileChooser.showSaveDialog(parent);
    if (retval == JFileChooser.APPROVE_OPTION) {
      try {
        if (!GraphExporters.exportAllAsSVG(
            graph, fileChooser.getSelectedFile().getAbsolutePath())) {
          throw new IOException("Failed to save SVG");
        }
      } catch (final IOException e) {
        CUtilityFunctions.logException(e);

        final String innerMessage = "E00195: " + "Could not save view to SVG file";
        final String innerDescription =
            CUtilityFunctions.createDescription(
                String.format(
                    "The view '%s' could not be written to the file '%s'.",
                    graph.getViewName(), fileChooser.getSelectedFile().getAbsolutePath()),
                new String[] {"There was a problem writing the PNG file."},
                new String[] {"The view was not written to the PNG file."});

        NaviErrorDialog.show(parent, innerMessage, innerDescription, e);
      }
    }
  }
  @Override
  public void setValueAt(final Object value, final int row, final int col) {
    if ((col != NAME_COLUMN) && (col != DESCRIPTION_COLUMN)) {
      throw new IllegalStateException("IE01161: Column can not be edited");
    }

    final INaviModule module = getModules().get(row);

    if (col == NAME_COLUMN) {
      try {
        module.getConfiguration().setName((String) value);
      } catch (final CouldntSaveDataException e) {
        CUtilityFunctions.logException(e);

        final String innerMessage = "E00156: " + "Could not save address space name";
        final String innerDescription =
            CUtilityFunctions.createDescription(
                String.format(
                    "The new name of the address space '%s' could not be saved.",
                    m_addressSpace.getConfiguration().getName()),
                new String[] {"There was a problem with the database connection."},
                new String[] {"The address space keeps its old name."});

        NaviErrorDialog.show(null, innerMessage, innerDescription, e);
      }
    } else if (col == DESCRIPTION_COLUMN) {
      try {
        module.getConfiguration().setDescription((String) value);
      } catch (final CouldntSaveDataException e) {
        CUtilityFunctions.logException(e);

        final String innerMessage = "E00157: " + "Could not save address space description";
        final String innerDescription =
            CUtilityFunctions.createDescription(
                String.format(
                    "The new description of the address space '%s' could not be saved.",
                    m_addressSpace.getConfiguration().getName()),
                new String[] {"There was a problem with the database connection."},
                new String[] {"The address space keeps its old description."});

        NaviErrorDialog.show(null, innerMessage, innerDescription, e);
      }
    }
  }
  /**
   * Creates the plugin menu.
   *
   * @return The created menu.
   */
  private JMenu createPluginsMenu() {
    final List<IMainWindowMenuPlugin> plugins = new ArrayList<IMainWindowMenuPlugin>();

    for (@SuppressWarnings("rawtypes")
    final IPlugin plugin : PluginInterface.instance().getPluginRegistry()) {
      if (plugin instanceof IMainWindowMenuPlugin) {
        plugins.add((IMainWindowMenuPlugin) plugin);
      }
    }

    final JMenu menu = new JMenu("Plugins");
    menu.setMnemonic('U');

    menu.add(CActionProxy.proxy(new CActionOpenScriptingDialog(getParent())));
    menu.add(CActionProxy.proxy(new CActionOpenLogConsole()));
    menu.addSeparator();
    menu.add(CActionProxy.proxy(new CPluginManagementAction(getParent())));
    menu.add(CActionProxy.proxy(new CPluginsReloadAction()));
    menu.addSeparator();

    for (final IMainWindowMenuPlugin plugin : plugins) {
      // ESCA-JAVA0166: Catch Exception because we are calling a plugin function.
      try {
        final List<JMenuItem> menus = plugin.extendPluginMenu();

        for (final JMenuItem m : menus) {
          menu.add(m);
        }
      } catch (final Exception exception) {
        CUtilityFunctions.logException(exception);

        final String innerMessage = "E00092: " + "Plugin caused an unexpected exception";
        final String innerDescription =
            CUtilityFunctions.createDescription(
                String.format("The plugin %s caused an unexpected exception.", plugin.getName()),
                new String[] {"The plugin contains a bug."},
                new String[] {
                  "The plugin probably behaves erroneously from this point on but it remains active"
                });

        NaviErrorDialog.show(getParent(), innerMessage, innerDescription, exception);
      }
    }

    return menu;
  }
    /**
     * Executes a script file without displaying it in the dialog.
     *
     * @param scriptFile The file to execute.
     */
    private void executeScriptFile(final File scriptFile) {
      final List<Pair<String, Object>> bindings = toPairList(m_bindings);

      final ScriptEngineManager manager = new ScriptEngineManager();

      final ScriptEngine engine =
          manager.getEngineByExtension(FileUtils.getFileExtension(scriptFile));

      final IScriptConsole console = new ConsoleWriter(new StringWriter());

      engine.getContext().setWriter(console.getWriter());

      bindings.add(new Pair<String, Object>("SCRIPT_CONSOLE", console));

      final ScriptThread thread = new ScriptThread(engine, scriptFile, bindings);

      CProgressDialog.showEndless(
          getOwner(), String.format("Executing '%s'", scriptFile.getAbsolutePath()), thread);

      if (thread.getException() != null) {
        CUtilityFunctions.logException(thread.getException());

        final String message = "E00108: " + "Script file could not be executed";
        final String description =
            CUtilityFunctions.createDescription(
                String.format(
                    "The script file '%s' could not be executed.", scriptFile.getAbsolutePath()),
                new String[] {
                  "The script file is in use by another program and can not be read.",
                  "You do not have sufficient rights to read the file",
                  "The script contains a bug that caused an exception."
                },
                new String[] {"BinNavi can not read the script file."});

        NaviErrorDialog.show(CScriptingDialog.this, message, description, thread.getException());
      }

      final IScriptPanel panel = (IScriptPanel) scriptTab.getSelectedComponent();

      panel.setOutput(console.getOutput());

      toFront();
    }
  /**
   * Loads the content of a database.
   *
   * @param parent Parent window used for dialogs.
   * @param database The database to load.
   */
  public static void loadDatabase(final Window parent, final IDatabase database) {
    final CDatabaseLoaderOperation operation = new CDatabaseLoaderOperation(database);

    try {
      database.connect();
      database.load();
    } catch (final CouldntLoadDriverException exception) {
      final String message = "E00012: " + "Database driver could not be loaded";
      final String description =
          CUtilityFunctions.createDescription(
              String.format(
                  "BinNavi could not create a database connection because the database "
                      + "driver '%s' could not be loaded",
                  database.getConfiguration().getDriver()),
              new String[] {
                "The database driver string is wrong.",
                "The database driver file could not be found."
              },
              new String[] {
                "BinNavi can not load data from the given database until the "
                    + "problem is resolved."
              });

      NaviErrorDialog.show(parent, message, description, exception);
    } catch (final CouldntLoadDataException exception) {
      final String message = "E00014: " + "Could not load data from the database";
      final String description =
          CUtilityFunctions.createDescription(
              "An error occurred when loading data from the database.",
              new String[] {
                "The connection to the database was dropped while the data was loaded.",
                "The database contains inconsistent information."
              },
              new String[] {
                "Close the database and open it again. Maybe close and re-start "
                    + "BinNavi too. If the program persists, please contact the BinNavi support."
              });

      NaviErrorDialog.show(parent, message, description, exception);
    } catch (final InvalidDatabaseException exception) {
      final String message = "E00015: " + "Database is in an inconsistent state";
      final String description =
          CUtilityFunctions.createDescription(
              "The selected database contains an invalid combination of BinNavi tables.",
              new String[] {
                "An earlier connection attempt failed and left the database in an "
                    + "inconsistent state.",
                "Some BinNavi tables were deleted accidentally by an outside program."
              },
              new String[] {
                "BinNavi can not use this database anymore. If the database is "
                    + "empty, please delete the database and create a new database to work with "
                    + "BinNavi. If the database already contains data please contact the BinNavi "
                    + "support."
              });

      NaviErrorDialog.show(parent, message, description, exception);
    } catch (final CouldntInitializeDatabaseException exception) {
      final String message = "E00016: Database could not be initialized";
      final String description =
          CUtilityFunctions.createDescription(
              "BinNavi could not initialize the tables required for storing disassembly data "
                  + "in the database.",
              new String[] {"There might have been a communication problem with the database."},
              new String[] {
                "The database is probably corrupted at this point. It is "
                    + "recommended to delete the database. Afterwards you can try again with a "
                    + "fresh database. If you do not want to do this please contact the BinNavi "
                    + "support to find out what other options exist for you."
              });

      NaviErrorDialog.show(parent, message, description, exception);
    } catch (final InvalidExporterDatabaseFormatException exception) {
      final String message = "E00017: " + "Database has invalid exporter tables";
      final String description =
          CUtilityFunctions.createDescription(
              "BinNavi could not load data from the selected database because the database "
                  + "contains invalid exporter tables",
              new String[] {"The database is too old to use with BinNavi."},
              new String[] {
                "It is recommended to create a database for this version of "
                    + "BinNavi. If you do not want to do this please contact the BinNavi support "
                    + "to find out what other options exist for you."
              });

      NaviErrorDialog.show(parent, message, description, exception);
    } catch (final InvalidDatabaseVersionException exception) {
      final String exceptionVersion = exception.getVersion().getString();
      if (!exceptionVersion.equals("4.0.0") || !exceptionVersion.equals("5.0.0")) {
        CMessageBox.showInformation(
            parent,
            String.format(
                "You are trying to connect to an outdated BinNavi %s database.\n\n"
                    + "Unfortunately you can not upgrade this database. Please create a "
                    + "new database and export your modules again.",
                exceptionVersion));
      } else {
        CMessageBox.showInformation(
            parent,
            String.format(
                "You are trying to connect to an outdated BinNavi %s database.\n\n"
                    + "You have the option to update the database.",
                exceptionVersion));

        if (JOptionPane.YES_OPTION
            == CMessageBox.showYesNoQuestion(
                parent,
                "Do you want to upgrade the database now?\n\n(The upgrade process can take "
                    + "very long depending on the size of your current database)\n\n(Make "
                    + "sure that the identity field contains a user name)")) {

          final CDefaultProgressOperation updateOperation =
              new CDefaultProgressOperation("Upgrading database", true, false);
          updateOperation.getProgressPanel().setText("Upgrading database");

          try {
            database.update();
            database.close();
          } catch (final CouldntUpdateDatabaseException upgradeException) {
            CUtilityFunctions.logException(upgradeException);

            final String message = "E00018: " + "Database could not be upgraded";
            final String description =
                CUtilityFunctions.createDescription(
                    String.format(
                        "BinNavi could not upgrade the database (database error %d). "
                            + "This is a serious problem because the database could be "
                            + "left in an inconsistent state. Please try to fix the "
                            + "problem that led to the error and try to update the "
                            + "database again.",
                        upgradeException.getErrorCode()),
                    new String[] {getErrorCode(upgradeException)},
                    new String[] {
                      "Please note that nobody must work with this database "
                          + "until the database conversion process is complete. If someone "
                          + "works with the database in its current state, partial or total "
                          + "data loss could happen."
                    });

            NaviErrorDialog.show(parent, message, description, exception);
          } finally {
            updateOperation.stop();
          }

          loadDatabase(parent, database);
        } else {
          database.close();
        }
      }

    } catch (final CouldntConnectException exception) {
      final CDatabaseConfiguration config = database.getConfiguration();
      if (exception.getSqlState().equalsIgnoreCase(PostgreSQLErrorCodes.INVALID_PASSWORD)) {
        CMessageBox.showInformation(
            parent,
            String.format(
                "The password for user '%s' on database '%s' is invalid",
                config.getUser(), config.getUrl()));
        return;
      }
      if (exception
          .getSqlState()
          .equalsIgnoreCase(PostgreSQLErrorCodes.POSTGRES_INVALID_CATALOG_NAME)) {

        if (JOptionPane.YES_OPTION
            == CMessageBox.showYesNoCancelQuestion(
                parent,
                String.format(
                    "The database '%s' does not exist. Do you want to create it now?",
                    config.getUrl()))) {
          CDatabaseCreator.createDatabase(parent, config);
        }
      } else {
        final String message = "E00013: Database connection could not be established";
        final String description =
            CUtilityFunctions.createDescription(
                String.format(
                    "BinNavi could not connect to the database '%s'",
                    database.getConfiguration().getName()),
                new String[] {exception.getMessage()},
                new String[] {
                  "BinNavi can not load data from the given database until the "
                      + "problem is resolved."
                });

        NaviErrorDialog.show(parent, message, description, exception);
      }
    } catch (final LoadCancelledException exception) {
      // We do not signal to the user that he cancelled loading.
    } finally {
      operation.stop();
    }
  }