Пример #1
0
 /**
  * @param bundleName The name of the resource bundle this toolbar will fetch the localizations
  *     from.
  */
 public OrigamistToolBar(String bundleName) {
   super();
   this.bundleName = bundleName;
   PropertyChangeListener l =
       new PropertyChangeListener() {
         @Override
         public void propertyChange(PropertyChangeEvent evt) {
           ResourceBundle oldAppMessages = messages;
           messages =
               ResourceBundle.getBundle(
                   OrigamistToolBar.this.bundleName, (Locale) evt.getNewValue());
           if (oldAppMessages == null || !oldAppMessages.equals(messages))
             firePropertyChange("messages", oldAppMessages, messages);
         }
       };
   ServiceLocator.get(ConfigurationManager.class).get().addPropertyChangeListener("locale", l);
   l.propertyChange(
       new PropertyChangeEvent(
           this,
           "locale",
           null,
           ServiceLocator.get(ConfigurationManager.class).get().getLocale()));
 }
Пример #2
0
  /**
   * @return The locales that should be topped. The default implementation returns the application
   *     and diagram locales from user settings along with all locales for which an entry exists in
   *     this input.
   */
  protected LinkedHashSet<Locale> getSuggestedLocales() {
    LinkedHashSet<Locale> result = new LinkedHashSet<Locale>(2);

    Configuration conf = ServiceLocator.get(ConfigurationManager.class).get();

    boolean isSetForLocale = false, isSetForDiagramLocale = false;

    Locale locale = conf.getLocale();
    for (LangString s : strings) {
      if (s.getLang().equals(locale)) {
        isSetForLocale = true;
        break;
      }
    }

    Locale diagramLocale = conf.getDiagramLocale();
    if (diagramLocale.equals(locale)) {
      isSetForDiagramLocale = isSetForLocale;
    } else {
      for (LangString s : strings) {
        if (s.getLang().equals(diagramLocale)) {
          isSetForDiagramLocale = true;
          break;
        }
      }
    }

    if (isSetForLocale) result.add(conf.getLocale());
    if (isSetForDiagramLocale) result.add(conf.getDiagramLocale());

    for (LangString s : strings) result.add(s.getLang());

    if (!isSetForLocale) result.add(conf.getLocale());
    if (!isSetForDiagramLocale) result.add(conf.getDiagramLocale());

    return result;
  }
 @Override
 protected String decorateTooltipText(String text) {
   return ServiceLocator.get(TooltipFactory.class).getPlain(text);
 }
Пример #4
0
  @Override
  public void actionPerformed(final ActionEvent e) {
    beforeAction(e);

    final ResourceBundle appMessages =
        ResourceBundle.getBundle(
            "application", ServiceLocator.get(ConfigurationManager.class).get().getLocale());

    final Component parent;
    if (e.getSource() instanceof Component) parent = (Component) e.getSource();
    else parent = null;

    JFileChooser chooser = new JFileChooser();
    File defaultFile =
        ServiceLocator.get(ConfigurationManager.class).get().getLastExportPath().getParentFile();
    chooser.setCurrentDirectory(defaultFile);
    chooser.setFileFilter(
        new FileNameExtensionFilter("*." + format.toString().toLowerCase(), format.toString()));
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.setApproveButtonText(appMessages.getString("exportDialog.approve"));
    chooser.setApproveButtonMnemonic(
        KeyStroke.getKeyStroke(appMessages.getString("exportDialog.approve.mnemonic"))
            .getKeyCode());
    chooser.setApproveButtonToolTipText(
        ServiceLocator.get(TooltipFactory.class)
            .getDecorated(
                appMessages.getString("exportDialog.approve.tooltip.message"),
                appMessages.getString("exportDialog.approve.tooltip.title"),
                "save.png",
                KeyStroke.getKeyStroke(
                    "alt " + appMessages.getString("exportDialog.approve.mnemonic"))));
    if (chooser.showDialog(parent, null) == JFileChooser.APPROVE_OPTION) {
      chooserApproved(e);

      File f = chooser.getSelectedFile();
      if (!chooser.accept(f)) {
        f = new File(f.toString() + "." + format.toString().toLowerCase());
      }
      ServiceLocator.get(ConfigurationManager.class).get().setLastExportPath(f);

      if (f.exists()) {
        if (JOptionPane.showConfirmDialog(
                parent,
                MessageFormat.format(
                    appMessages.getString("exportDialog.overwrite"), new Object[] {f}),
                appMessages.getString("exportDialog.overwrite.title"),
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null)
            != JOptionPane.YES_OPTION) {
          return;
        }
      }

      final File file = f;

      final ExportOptions options = format.askForOptions(origami);

      final JDialog progressDialog = new JDialog();
      final JProgressBar bar = new JProgressBar(0, format.getNumOfProgressChunks(origami, options));
      bar.setStringPainted(true);
      bar.setString(appMessages.getString("exporting.no.percentage"));
      progressDialog.getContentPane().setLayout(new FlowLayout());
      progressDialog.setUndecorated(true);
      progressDialog.setAlwaysOnTop(true);
      progressDialog.setLocationRelativeTo(null);

      final SwingWorker<Set<File>, Void> worker =
          new SwingWorker<Set<File>, Void>() {

            private int chunksReceived = 0;

            @Override
            protected Set<File> doInBackground() throws Exception {
              final Runnable progressCallback =
                  new Runnable() {
                    @Override
                    public void run() {
                      publish(new Void[] {null});
                      if (isCancelled()) throw new RuntimeException();
                    }
                  };

              return ServiceLocator.get(OrigamiHandler.class)
                  .export(origami, file, format, options, progressCallback);
            }

            @Override
            protected void process(List<Void> chunks) {
              chunksReceived += chunks.size();
              bar.setValue(chunksReceived);
              bar.setString(
                  MessageFormat.format(
                      appMessages.getString("exporting.percentage"), bar.getPercentComplete()));
            }

            @Override
            protected void done() {
              progressDialog.setVisible(false);
              progressDialog.dispose();
              try {
                if (!isCancelled()) {
                  Set<File> resultFiles = get();
                  StringBuilder b = new StringBuilder();
                  for (File f : resultFiles) b.append("\n").append(f.toString());
                  JOptionPane.showMessageDialog(
                      parent,
                      MessageFormat.format(
                          appMessages.getString("exportSuccessful.message"),
                          new Object[] {b.toString(), resultFiles.size()}),
                      appMessages.getString("exportSuccessful.title"),
                      JOptionPane.INFORMATION_MESSAGE,
                      null);
                }
              } catch (HeadlessException e) {
                JOptionPane.showMessageDialog(
                    parent,
                    MessageFormat.format(
                        appMessages.getString("failedToExport.message"),
                        new Object[] {file.toString()}),
                    appMessages.getString("failedToExport.title"),
                    JOptionPane.ERROR_MESSAGE,
                    null);
                Logger.getLogger("application").warn("Unable to export origami.", e);
              } catch (InterruptedException e) {
                JOptionPane.showMessageDialog(
                    parent,
                    MessageFormat.format(
                        appMessages.getString("failedToExport.message"),
                        new Object[] {file.toString()}),
                    appMessages.getString("failedToExport.title"),
                    JOptionPane.ERROR_MESSAGE,
                    null);
                Logger.getLogger("application").warn("Unable to export origami.", e);
              } catch (ExecutionException e) {
                JOptionPane.showMessageDialog(
                    parent,
                    MessageFormat.format(
                        appMessages.getString("failedToExport.message"),
                        new Object[] {file.toString()}),
                    appMessages.getString("failedToExport.title"),
                    JOptionPane.ERROR_MESSAGE,
                    null);
                Logger.getLogger("application").warn("Unable to export origami.", e);
              }
              ExportAction.this.done(e);
            }
          };

      progressDialog.getContentPane().add(bar);
      JButton cancel = new JButton();
      cancel.setAction(
          new AbstractAction() {
            /** */
            private static final long serialVersionUID = 104733262578076493L;

            @Override
            public void actionPerformed(ActionEvent e) {
              worker.cancel(true);
            }
          });
      cancel.setText(appMessages.getString("buttons.cancel"));
      progressDialog.getContentPane().add(cancel);

      progressDialog.pack();

      progressDialog.setVisible(true);
      worker.execute();
    } else {
      if (e.getSource() instanceof JComponent)
        ((JComponent) e.getSource()).setCursor(Cursor.getDefaultCursor());
    }
  }