예제 #1
0
  void onOpenFileClicked() {
    if (!maybeSave()) {
      return;
    }

    try {
      JFileChooser fc = new JFileChooser();
      FileNameExtensionFilter filter1 =
          new FileNameExtensionFilter(strings.getString("filetype." + EXTENSION), EXTENSION);
      fc.setFileFilter(filter1);

      int rv = fc.showOpenDialog(this);
      if (rv == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        Tournament t = new Tournament();
        t.loadFile(file);

        setTournament(file, t);
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          this, e, strings.getString("main.error_caption"), JOptionPane.ERROR_MESSAGE);
    }
  }
예제 #2
0
  /** Install Add and Remove Buttons into the toolbar */
  private void installAddRemovePointButtons() {
    URL imgURL = ClassLoader.getSystemResource("ch/tbe/pics/plus.gif");
    ImageIcon plus = new ImageIcon(imgURL);
    imgURL = ClassLoader.getSystemResource("ch/tbe/pics/minus.gif");
    ImageIcon minus = new ImageIcon(imgURL);
    add = new JButton(plus);
    rem = new JButton(minus);
    add.setToolTipText(workingViewLabels.getString("plus"));
    rem.setToolTipText(workingViewLabels.getString("minus"));
    add.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            WorkingView.this.addRemovePoint(true);
          }
        });
    rem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            WorkingView.this.addRemovePoint(false);
          }
        });

    add.setContentAreaFilled(false);
    add.setBorderPainted(false);
    rem.setContentAreaFilled(false);
    rem.setBorderPainted(false);
    toolbar.add(add);
    toolbar.add(rem);
  }
예제 #3
0
 public ExitAction(MainFrame main) {
   super();
   Locale locale = Locale.getDefault();
   bundle = ResourceBundle.getBundle(getClass().getName(), locale);
   this.main = main;
   putValue(NAME, bundle.getString("Exit"));
   putValue(MNEMONIC_KEY, new Integer(bundle.getString("Exit.mnemonic").charAt(0)));
 }
예제 #4
0
 void refresh() {
   if (currentFile != null) {
     String fileName = currentFile.getName();
     if (fileName.endsWith("." + EXTENSION)) {
       fileName = fileName.substring(0, fileName.length() - 1 - EXTENSION.length());
     }
     setTitle(MessageFormat.format(strings.getString("main.caption_named_file"), fileName));
   } else {
     setTitle(strings.getString("main.caption_unnamed_file"));
   }
 }
 protected void initResourceBundle(UIDefaults table) {
   // The following line of code does not work, when Quaqua has been loaded with
   // a custom class loader. That's why, we have to inject the labels
   // by ourselves:
   // table.addResourceBundle( "ch.randelshofer.quaqua.Labels" );
   ResourceBundle bundle =
       ResourceBundle.getBundle(
           "ch.randelshofer.quaqua.Labels", Locale.getDefault(), getClass().getClassLoader());
   for (Enumeration i = bundle.getKeys(); i.hasMoreElements(); ) {
     String key = (String) i.nextElement();
     table.put(key, bundle.getObject(key));
   }
 }
예제 #6
0
  /**
   * Output the specified {@link Collection} in proper columns.
   *
   * @param stuff the stuff to print
   */
  public void printColumns(final Collection stuff) throws IOException {
    if ((stuff == null) || (stuff.size() == 0)) {
      return;
    }

    int width = getTermwidth();
    int maxwidth = 0;

    for (Iterator i = stuff.iterator();
        i.hasNext();
        maxwidth = Math.max(maxwidth, i.next().toString().length())) {;
    }

    StringBuffer line = new StringBuffer();

    int showLines;

    if (usePagination) showLines = getTermheight() - 1; // page limit
    else showLines = Integer.MAX_VALUE;

    for (Iterator i = stuff.iterator(); i.hasNext(); ) {
      String cur = (String) i.next();

      if ((line.length() + maxwidth) > width) {
        printString(line.toString().trim());
        printNewline();
        line.setLength(0);
        if (--showLines == 0) { // Overflow
          printString(loc.getString("display-more"));
          flushConsole();
          int c = readVirtualKey();
          if (c == '\r' || c == '\n') showLines = 1; // one step forward
          else if (c != 'q') showLines = getTermheight() - 1; // page forward

          back(loc.getString("display-more").length());
          if (c == 'q') break; // cancel
        }
      }

      pad(cur, maxwidth + 3, line);
    }

    if (line.length() > 0) {
      printString(line.toString().trim());
      printNewline();
      line.setLength(0);
    }
  }
예제 #7
0
 public String getLevelString(String code) {
   try {
     return mResource.getString(LEVEL_PROPERTY + code);
   } catch (MissingResourceException e) {
     return code;
   }
 }
  /** Creates a new instance of clPrintGraphPanel */
  public clPrintGraphPanel(java.awt.Frame parent, clHauptPanel hp, Locale lc) {
    super(hp.getKn(), hp.getSt(), true);
    this.parent = parent;
    dxf = hp.dxf;
    mechanismusRelKnVersch = hp.mechanismusRelKnVersch;

    Point2D[] zoomPkte = hp.getZoomPkte();
    ZoomPkt1 = zoomPkte[0];
    ZoomPkt2 = zoomPkte[1];
    ZOOMALL = hp.ZOOMALL;
    maxMechSkal = hp.maxMechSkal;

    boolean[] aktiveLayer = hp.getAktiveLayer();
    MIT_KnNr = aktiveLayer[0];
    MIT_StabNr = aktiveLayer[1];
    MIT_Lasten = aktiveLayer[2];
    MIT_Auflagerkräften = aktiveLayer[3];
    MIT_Stabkräften = aktiveLayer[4];
    MIT_Hintergrund = aktiveLayer[5];
    MIT_Mechanismus = aktiveLayer[6];

    maxPfeil = maxPfeil * druckvergr;
    spitzenlängeMax = spitzenlängeMax * druckvergr; // / (0.5d + printskal/2d);
    spitzenlängeMin = spitzenlängeMin * druckvergr; // / (0.5d + printskal/2d);
    lagerhöhe = lagerhöhe * (float) druckvergr; // / (0.5f + ((float)printskal)/2f);

    locale = lc;
    druckRB = ResourceBundle.getBundle("Fachwerk/locales/gui-drucken", locale);
    if (druckRB == null) {
      System.err.println("FEHLER: gui-drucken für " + locale.toString());
    }
  }
예제 #9
0
 public String getSourceString(String code) {
   try {
     return mResource.getString(SOURCE_PROPERTY + code);
   } catch (MissingResourceException e) {
     return code;
   }
 }
예제 #10
0
 protected String getResourceString(String nm) {
   String str;
   try {
     str = resources.getString(nm);
   } catch (MissingResourceException mre) {
     str = null;
   }
   return str;
 }
예제 #11
0
 static {
   try {
     resources = ResourceBundle.getBundle("resources.TextViewer", Locale.getDefault());
   } catch (MissingResourceException mre) {
     String errstr = "TextViewer:resources/TextViewer.properties not found";
     // System.exit(1);
     System.err.println(errstr);
   }
 }
예제 #12
0
 static {
   try {
     properties = new Properties();
     properties.load(Notepad.class.getResourceAsStream("resources/NotepadSystem.properties"));
     resources = ResourceBundle.getBundle("resources.Notepad", Locale.getDefault());
   } catch (MissingResourceException | IOException e) {
     System.err.println(
         "resources/Notepad.properties " + "or resources/NotepadSystem.properties not found");
     System.exit(1);
   }
 }
예제 #13
0
  public void actionPerformed(ActionEvent e) {
    // Ask user to confirm exit.
    int reply =
        JOptionPane.showConfirmDialog(
            main,
            bundle.getString("Exit_application_?"),
            bundle.getString("Exit"),
            JOptionPane.YES_NO_OPTION);
    if (reply != JOptionPane.YES_OPTION) return;

    // Save MainFrame's bounds
    Rectangle r = main.getBounds();
    Main.setProperty("window.bounds.width", Integer.toString(r.width));
    Main.setProperty("window.bounds.height", Integer.toString(r.height));
    Main.setProperty("window.bounds.x", Integer.toString(r.x));
    Main.setProperty("window.bounds.y", Integer.toString(r.y));

    // System Exit
    Main.saveProperties();
    Main.exit(0);
  }
  /**
   * Create this dialog with the given parent and title.
   *
   * @param parent window from which this dialog is launched
   * @param title the title for the dialog box window
   * @since ostermillerutils 1.00.00
   */
  public PasswordDialog(Frame parent, String title) {

    super(parent, title, true);

    setLocale(Locale.getDefault());

    if (title == null) {
      setTitle(labels.getString("dialog.title"));
    }
    if (parent != null) {
      setLocationRelativeTo(parent);
    }
    // super calls dialogInit, so we don't need to do it again.
  }
예제 #15
0
  /** @return true if file was saved, false if user canceled */
  boolean onSaveFileClicked() {
    if (currentFile == null) {
      return onSaveAsFileClicked();
    }

    try {
      tournament.saveFile(currentFile);
      return true;
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          this, e, strings.getString("main.error_caption"), JOptionPane.ERROR_MESSAGE);
    }
    return false;
  }
예제 #16
0
  /** @return true if file was saved, false if user canceled */
  boolean onSaveAsFileClicked() {
    try {

      JFileChooser fc = new JFileChooser();
      FileNameExtensionFilter filter1 =
          new FileNameExtensionFilter(strings.getString("filetype." + EXTENSION), EXTENSION);
      fc.setFileFilter(filter1);
      int rv = fc.showSaveDialog(this);

      if (rv == JFileChooser.APPROVE_OPTION) {
        currentFile = fc.getSelectedFile();
        if (!currentFile.getName().endsWith("." + EXTENSION)) {
          currentFile = new File(currentFile.getPath() + "." + EXTENSION);
        }
        doSave(currentFile);
        refresh();
        return true;
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          this, e, strings.getString("main.error_caption"), JOptionPane.ERROR_MESSAGE);
    }
    return false;
  }
 private String tr(String key) {
   String übersetzt;
   try {
     übersetzt = druckRB.getString(key);
   } catch (MissingResourceException e) {
     System.err.println(
         "Schluesselwort "
             + key
             + " nicht gefunden fuer "
             + locale.toString()
             + " ; "
             + e.toString());
     return key;
   }
   return übersetzt;
 }
예제 #18
0
  void makeMenu() {
    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu(strings.getString("menu.file"));
    menuBar.add(fileMenu);

    JMenuItem menuItem;
    menuItem = new JMenuItem(strings.getString("menu.file.new"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onNewFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.open"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onOpenFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.save"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onSaveFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.save_as"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onSaveAsFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.exit"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            closeWindow();
          }
        });
    fileMenu.add(menuItem);

    setJMenuBar(menuBar);
  }
예제 #19
0
 /**
  * ** Constructor ** @param pkgName The resource package name ** @param loc The Locale resource
  * from with the localized text is loaded
  */
 private I18N(String pkgName, Locale loc) {
   String bundleName = null;
   try {
     this.locale = I18N.getLocale(loc);
     bundleName =
         ((pkgName == null) || pkgName.equals("")) ? LOCAL_STRINGS : (pkgName + _LOCAL_STRINGS);
     this.resBundle = ResourceBundle.getBundle(bundleName, this.locale);
     // Print.logInfo("Found bundle: " + bundleName);
   } catch (Throwable th) {
     // MissingResourceException
     if (loc != null) {
       // quietly ignore this exception if (loc == null)
       Print.logInfo("Bundle not found: " + bundleName + " [" + th);
     }
     this.resBundle = null;
   }
 }
예제 #20
0
  boolean maybeSave() {
    if (tournament.isDirty()) {

      int rv =
          JOptionPane.showConfirmDialog(
              this,
              strings.getString("main.save_query"),
              PRODUCT_NAME,
              JOptionPane.YES_NO_CANCEL_OPTION,
              JOptionPane.WARNING_MESSAGE);
      if (rv == JOptionPane.CANCEL_OPTION) return false;

      if (rv == JOptionPane.YES_OPTION) {
        return onSaveFileClicked();
      }
    }
    return true;
  }
예제 #21
0
  private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    // Generated using JFormDesigner Open Source Project license - unknown
    ResourceBundle bundle = ResourceBundle.getBundle("InformationDialog");
    JPanel dialogPane = new JPanel();
    JPanel contentPanel = new JPanel();
    iconLabel = new JLabel();
    pathLabel = new JLabel();
    JLabel labelFrom = new JLabel();
    fieldFrom = new JTextField();
    JLabel labelSize = new JLabel();
    fieldSize = new JTextField();
    JLabel labelDescription = new JLabel();
    JScrollPane scrollPane1 = new JScrollPane();
    descriptionArea = ComponentFactory.getTextArea();
    JPanel optionsPanel = new JPanel();
    JLabel saveToLabel = new JLabel();
    comboPath = new JComboBox();
    btnSelectPath = new JButton();
    progressBar = new JProgressBar();
    JLabel labelRemaining = new JLabel();
    remainingLabel = new JLabel();
    JLabel labelEstimateTime = new JLabel();
    estTimeLabel = new JLabel();
    JLabel labelCurrentSpeed = new JLabel();
    currentSpeedLabel = new JLabel();
    JLabel labelAverageSpeed = new JLabel();
    avgSpeedLabel = new JLabel();
    JXButtonPanel buttonBar = new JXButtonPanel();
    okButton = new JButton();
    cancelButton = new JButton();
    CellConstraints cc = new CellConstraints();

    // ======== this ========
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());

    // ======== dialogPane ========
    {
      dialogPane.setBorder(Borders.DIALOG);
      dialogPane.setLayout(new BorderLayout());

      // ======== contentPanel ========
      {

        // ---- iconLabel ----
        iconLabel.setText(bundle.getString("iconLabel.text"));

        // ---- pathLabel ----
        pathLabel.setText(bundle.getString("pathLabel.text"));
        pathLabel.setFont(new Font("Tahoma", Font.BOLD, 12));

        // ---- labelFrom ----
        labelFrom.setText(bundle.getString("labelFrom.text"));

        // ---- fieldFrom ----
        fieldFrom.setBorder(null);
        fieldFrom.setOpaque(false);
        fieldFrom.setText(bundle.getString("fieldFrom.text"));

        // ---- labelSize ----
        labelSize.setText(bundle.getString("labelSize.text"));

        // ---- fieldSize ----
        fieldSize.setBorder(null);
        fieldSize.setOpaque(false);

        // ---- labelDescription ----
        labelDescription.setText(bundle.getString("labelDescription.text"));

        // ======== scrollPane1 ========
        {
          scrollPane1.setViewportView(descriptionArea);
        }

        // ======== optionsPanel ========
        {

          // ---- saveToLabel ----
          saveToLabel.setText(bundle.getString("saveToLabel.text"));
          saveToLabel.setLabelFor(comboPath);

          // ---- comboPath ----
          comboPath.setEditable(true);

          // ---- btnSelectPath ----
          btnSelectPath.setText(bundle.getString("btnSelectPath.text"));

          PanelBuilder optionsPanelBuilder =
              new PanelBuilder(
                  new FormLayout(
                      new ColumnSpec[] {
                        FormSpecs.DEFAULT_COLSPEC,
                        FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                        new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
                        FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                        FormSpecs.DEFAULT_COLSPEC
                      },
                      RowSpec.decodeSpecs("default")),
                  optionsPanel);

          optionsPanelBuilder.add(saveToLabel, cc.xy(1, 1));
          optionsPanelBuilder.add(comboPath, cc.xy(3, 1));
          optionsPanelBuilder.add(btnSelectPath, cc.xy(5, 1));
        }

        // ---- progressBar ----
        progressBar.setFont(new Font("Tahoma", Font.BOLD, 16));

        // ---- labelRemaining ----
        labelRemaining.setText(bundle.getString("labelRemaining.text"));

        // ---- remainingLabel ----
        remainingLabel.setText(bundle.getString("remainingLabel.text"));
        remainingLabel.setFont(new Font("Tahoma", Font.BOLD, 12));

        // ---- labelEstimateTime ----
        labelEstimateTime.setText(bundle.getString("labelEstimateTime.text"));

        // ---- estTimeLabel ----
        estTimeLabel.setText(bundle.getString("estTimeLabel.text"));
        estTimeLabel.setFont(new Font("Tahoma", Font.BOLD, 12));

        // ---- labelCurrentSpeed ----
        labelCurrentSpeed.setText(bundle.getString("labelCurrentSpeed.text"));

        // ---- currentSpeedLabel ----
        currentSpeedLabel.setText(bundle.getString("currentSpeedLabel.text"));
        currentSpeedLabel.setFont(new Font("Tahoma", Font.BOLD, 12));

        // ---- labelAverageSpeed ----
        labelAverageSpeed.setText(bundle.getString("labelAverageSpeed.text"));

        // ---- avgSpeedLabel ----
        avgSpeedLabel.setText(bundle.getString("avgSpeedLabel.text"));
        avgSpeedLabel.setFont(new Font("Tahoma", Font.BOLD, 12));

        PanelBuilder contentPanelBuilder =
            new PanelBuilder(
                new FormLayout(
                    new ColumnSpec[] {
                      new ColumnSpec(Sizes.dluX(49)),
                      FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                      FormSpecs.DEFAULT_COLSPEC,
                      FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                      new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
                      FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                      FormSpecs.DEFAULT_COLSPEC,
                      FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                      ColumnSpec.decode("max(min;70dlu)")
                    },
                    new RowSpec[] {
                      FormSpecs.DEFAULT_ROWSPEC,
                      FormSpecs.LINE_GAP_ROWSPEC,
                      FormSpecs.DEFAULT_ROWSPEC,
                      FormSpecs.LINE_GAP_ROWSPEC,
                      FormSpecs.DEFAULT_ROWSPEC,
                      FormSpecs.LINE_GAP_ROWSPEC,
                      FormSpecs.DEFAULT_ROWSPEC,
                      FormSpecs.LINE_GAP_ROWSPEC,
                      new RowSpec(
                          RowSpec.FILL,
                          Sizes.bounded(Sizes.PREFERRED, Sizes.dluY(40), Sizes.dluY(50)),
                          FormSpec.DEFAULT_GROW),
                      FormSpecs.LINE_GAP_ROWSPEC,
                      FormSpecs.DEFAULT_ROWSPEC,
                      FormSpecs.LINE_GAP_ROWSPEC,
                      RowSpec.decode("fill:max(pref;20dlu)"),
                      FormSpecs.LINE_GAP_ROWSPEC,
                      FormSpecs.DEFAULT_ROWSPEC,
                      FormSpecs.LINE_GAP_ROWSPEC,
                      FormSpecs.DEFAULT_ROWSPEC
                    }),
                contentPanel);

        contentPanelBuilder.add(iconLabel, cc.xywh(1, 1, 1, 5));
        contentPanelBuilder.add(pathLabel, cc.xywh(3, 1, 7, 1));
        contentPanelBuilder.add(labelFrom, cc.xy(3, 3));
        contentPanelBuilder.add(fieldFrom, cc.xywh(5, 3, 5, 1));
        contentPanelBuilder.add(labelSize, cc.xy(3, 5));
        contentPanelBuilder.add(fieldSize, cc.xywh(5, 5, 3, 1));
        contentPanelBuilder.add(labelDescription, cc.xy(1, 7));
        contentPanelBuilder.add(scrollPane1, cc.xywh(1, 9, 9, 1));
        contentPanelBuilder.add(optionsPanel, cc.xywh(1, 11, 9, 1));
        contentPanelBuilder.add(progressBar, cc.xywh(1, 13, 9, 1));
        contentPanelBuilder.add(labelRemaining, cc.xy(1, 15));
        contentPanelBuilder.add(remainingLabel, cc.xywh(3, 15, 3, 1));
        contentPanelBuilder.add(labelEstimateTime, cc.xy(7, 15));
        contentPanelBuilder.add(estTimeLabel, cc.xy(9, 15));
        contentPanelBuilder.add(labelCurrentSpeed, cc.xy(1, 17));
        contentPanelBuilder.add(currentSpeedLabel, cc.xywh(3, 17, 3, 1));
        contentPanelBuilder.add(labelAverageSpeed, cc.xy(7, 17));
        contentPanelBuilder.add(avgSpeedLabel, cc.xy(9, 17));
      }
      dialogPane.add(contentPanel, BorderLayout.CENTER);

      // ======== buttonBar ========
      {
        buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));

        // ---- okButton ----
        okButton.setText(bundle.getString("okButton.text"));

        // ---- cancelButton ----
        cancelButton.setText(bundle.getString("cancelButton.text"));

        PanelBuilder buttonBarBuilder =
            new PanelBuilder(
                new FormLayout(
                    new ColumnSpec[] {
                      new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
                      FormSpecs.UNRELATED_GAP_COLSPEC,
                      ColumnSpec.decode("max(pref;55dlu)"),
                      FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                      FormSpecs.DEFAULT_COLSPEC
                    },
                    RowSpec.decodeSpecs("fill:pref")),
                buttonBar);
        ((FormLayout) buttonBar.getLayout()).setColumnGroups(new int[][] {{3, 5}});

        buttonBarBuilder.add(okButton, cc.xy(3, 1));
        buttonBarBuilder.add(cancelButton, cc.xy(5, 1));
      }
      dialogPane.add(buttonBar, BorderLayout.SOUTH);
    }
    contentPane.add(dialogPane, BorderLayout.CENTER);
    pack();
    setLocationRelativeTo(getOwner());
    // JFormDesigner - End of component initialization  //GEN-END:initComponents
  }
예제 #22
0
public class MainWindow extends JFrame {
  static ResourceBundle strings = ResourceBundle.getBundle("dragonfin.tournament.GuiStrings");
  static final String PRODUCT_NAME = strings.getString("PRODUCT");
  static final String EXTENSION = "tourney";

  Tournament tournament;
  File currentFile;

  RosterModel rosterModel;
  JTable rosterTable;
  JScrollPane rosterScrollPane;

  public MainWindow() {
    setTitle(PRODUCT_NAME);

    rosterTable = new JTable();

    rosterScrollPane = new JScrollPane(rosterTable);
    rosterTable.setFillsViewportHeight(true);
    getContentPane().add(rosterScrollPane, BorderLayout.CENTER);

    makeMenu();
    pack();
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    setLocationRelativeTo(null);

    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent ev) {
            closeWindow();
          }
        });

    setTournament(null, new Tournament());
  }

  void makeMenu() {
    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu(strings.getString("menu.file"));
    menuBar.add(fileMenu);

    JMenuItem menuItem;
    menuItem = new JMenuItem(strings.getString("menu.file.new"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onNewFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.open"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onOpenFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.save"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onSaveFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.save_as"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            onSaveAsFileClicked();
          }
        });
    fileMenu.add(menuItem);

    menuItem = new JMenuItem(strings.getString("menu.file.exit"));
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            closeWindow();
          }
        });
    fileMenu.add(menuItem);

    setJMenuBar(menuBar);
  }

  void closeWindow() {
    if (!maybeSave()) {
      return;
    }

    dispose();
  }

  boolean maybeSave() {
    if (tournament.isDirty()) {

      int rv =
          JOptionPane.showConfirmDialog(
              this,
              strings.getString("main.save_query"),
              PRODUCT_NAME,
              JOptionPane.YES_NO_CANCEL_OPTION,
              JOptionPane.WARNING_MESSAGE);
      if (rv == JOptionPane.CANCEL_OPTION) return false;

      if (rv == JOptionPane.YES_OPTION) {
        return onSaveFileClicked();
      }
    }
    return true;
  }

  void onNewFileClicked() {
    if (!maybeSave()) {
      return;
    }

    setTournament(null, new Tournament());
  }

  void doSave(File file) throws IOException {
    currentFile = file;
    tournament.saveFile(file);
  }

  void onOpenFileClicked() {
    if (!maybeSave()) {
      return;
    }

    try {
      JFileChooser fc = new JFileChooser();
      FileNameExtensionFilter filter1 =
          new FileNameExtensionFilter(strings.getString("filetype." + EXTENSION), EXTENSION);
      fc.setFileFilter(filter1);

      int rv = fc.showOpenDialog(this);
      if (rv == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();

        Tournament t = new Tournament();
        t.loadFile(file);

        setTournament(file, t);
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          this, e, strings.getString("main.error_caption"), JOptionPane.ERROR_MESSAGE);
    }
  }

  /** @return true if file was saved, false if user canceled */
  boolean onSaveFileClicked() {
    if (currentFile == null) {
      return onSaveAsFileClicked();
    }

    try {
      tournament.saveFile(currentFile);
      return true;
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          this, e, strings.getString("main.error_caption"), JOptionPane.ERROR_MESSAGE);
    }
    return false;
  }

  /** @return true if file was saved, false if user canceled */
  boolean onSaveAsFileClicked() {
    try {

      JFileChooser fc = new JFileChooser();
      FileNameExtensionFilter filter1 =
          new FileNameExtensionFilter(strings.getString("filetype." + EXTENSION), EXTENSION);
      fc.setFileFilter(filter1);
      int rv = fc.showSaveDialog(this);

      if (rv == JFileChooser.APPROVE_OPTION) {
        currentFile = fc.getSelectedFile();
        if (!currentFile.getName().endsWith("." + EXTENSION)) {
          currentFile = new File(currentFile.getPath() + "." + EXTENSION);
        }
        doSave(currentFile);
        refresh();
        return true;
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          this, e, strings.getString("main.error_caption"), JOptionPane.ERROR_MESSAGE);
    }
    return false;
  }

  void refresh() {
    if (currentFile != null) {
      String fileName = currentFile.getName();
      if (fileName.endsWith("." + EXTENSION)) {
        fileName = fileName.substring(0, fileName.length() - 1 - EXTENSION.length());
      }
      setTitle(MessageFormat.format(strings.getString("main.caption_named_file"), fileName));
    } else {
      setTitle(strings.getString("main.caption_unnamed_file"));
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            new MainWindow().setVisible(true);
          }
        });
  }

  public void setTournament(File file, Tournament newTournament) {
    assert newTournament != null;

    this.currentFile = file;
    this.tournament = newTournament;

    rosterModel = new RosterModel(newTournament);
    rosterTable.setModel(rosterModel);

    refresh();
  }
}
  private int extractExpectedHighlight(
      final Matcher matcher,
      final String text,
      final Document document,
      final Ref<Integer> textOffset) {
    document.deleteString(textOffset.get(), textOffset.get() + matcher.end() - matcher.start());

    int groupIdx = 1;
    final String marker = matcher.group(groupIdx++);
    String descr = matcher.group(groupIdx++);
    final String typeString = matcher.group(groupIdx++);
    final String foregroundColor = matcher.group(groupIdx++);
    final String backgroundColor = matcher.group(groupIdx++);
    final String effectColor = matcher.group(groupIdx++);
    final String effectType = matcher.group(groupIdx++);
    final String fontType = matcher.group(groupIdx++);
    final String attrKey = matcher.group(groupIdx++);
    final String bundleMessage = matcher.group(groupIdx++);
    final boolean closed = matcher.group(groupIdx) != null;

    if (descr == null) {
      descr = ANY_TEXT; // no descr means any string by default
    } else if (descr.equals("null")) {
      descr = null; // explicit "null" descr
    }
    if (descr != null) {
      descr =
          descr.replaceAll(
              "\\\\\\\\\"", "\""); // replace: \\" to ", doesn't check symbol before sequence \\"
      descr = descr.replaceAll("\\\\\"", "\"");
    }

    HighlightInfoType type = WHATEVER;
    if (typeString != null) {
      try {
        type = getTypeByName(typeString);
      } catch (Exception e) {
        LOG.error(e);
      }
      LOG.assertTrue(type != null, "Wrong highlight type: " + typeString);
    }

    TextAttributes forcedAttributes = null;
    if (foregroundColor != null) {
      //noinspection MagicConstant
      forcedAttributes =
          new TextAttributes(
              Color.decode(foregroundColor),
              Color.decode(backgroundColor),
              Color.decode(effectColor),
              EffectType.valueOf(effectType),
              Integer.parseInt(fontType));
    }

    final int rangeStart = textOffset.get();
    final int toContinueFrom;
    if (closed) {
      toContinueFrom = matcher.end();
    } else {
      int pos = matcher.end();
      final Matcher closingTagMatcher = Pattern.compile("</" + marker + ">").matcher(text);
      while (true) {
        if (!closingTagMatcher.find(pos)) {
          LOG.error("Cannot find closing </" + marker + "> in position " + pos);
        }

        final int nextTagStart = matcher.find(pos) ? matcher.start() : text.length();
        if (closingTagMatcher.start() < nextTagStart) {
          textOffset.set(textOffset.get() + closingTagMatcher.start() - pos);
          document.deleteString(
              textOffset.get(),
              textOffset.get() + closingTagMatcher.end() - closingTagMatcher.start());
          toContinueFrom = closingTagMatcher.end();
          break;
        }

        textOffset.set(textOffset.get() + nextTagStart - pos);
        pos = extractExpectedHighlight(matcher, text, document, textOffset);
      }
    }

    final ExpectedHighlightingSet expectedHighlightingSet = myHighlightingTypes.get(marker);
    if (expectedHighlightingSet.enabled) {
      TextAttributesKey forcedTextAttributesKey =
          attrKey == null ? null : TextAttributesKey.createTextAttributesKey(attrKey);
      HighlightInfo.Builder builder =
          HighlightInfo.newHighlightInfo(type)
              .range(rangeStart, textOffset.get())
              .severity(expectedHighlightingSet.severity);

      if (forcedAttributes != null) builder.textAttributes(forcedAttributes);
      if (forcedTextAttributesKey != null) builder.textAttributes(forcedTextAttributesKey);
      if (bundleMessage != null) {
        final List<String> split = StringUtil.split(bundleMessage, "|");
        final ResourceBundle bundle = ResourceBundle.getBundle(split.get(0));
        descr = CommonBundle.message(bundle, split.get(1), split.stream().skip(2).toArray());
      }
      if (descr != null) {
        builder.description(descr);
        builder.unescapedToolTip(descr);
      }
      if (expectedHighlightingSet.endOfLine) builder.endOfLine();
      HighlightInfo highlightInfo = builder.createUnconditionally();
      expectedHighlightingSet.infos.add(highlightInfo);
    }

    return toContinueFrom;
  }
 /**
  * Set the locale used for getting localized strings.
  *
  * @param locale Locale used to for i18n.
  * @since ostermillerutils 1.00.00
  */
 @Override
 public void setLocale(Locale locale) {
   labels = ResourceBundle.getBundle("com.Ostermiller.util.PasswordDialog", locale);
 }
예제 #25
0
/**
 * A reader for console applications. It supports custom tab-completion, saveable command history,
 * and command line editing. On some platforms, platform-specific commands will need to be issued
 * before the reader will function properly. See {@link Terminal#initializeTerminal} for convenience
 * methods for issuing platform-specific setup commands.
 *
 * @author <a href="mailto:[email protected]">Marc Prud'hommeaux</a>
 */
public class ConsoleReader implements ConsoleOperations {

  static final int TAB_WIDTH = 4;

  String prompt;

  private boolean useHistory = true;

  private boolean usePagination = false;

  public static final String CR = System.getProperty("line.separator");

  private static ResourceBundle loc =
      ResourceBundle.getBundle(CandidateListCompletionHandler.class.getName());

  /** Map that contains the operation name to keymay operation mapping. */
  public static SortedMap KEYMAP_NAMES;

  static {
    Map names = new TreeMap();

    names.put("MOVE_TO_BEG", new Short(MOVE_TO_BEG));
    names.put("MOVE_TO_END", new Short(MOVE_TO_END));
    names.put("PREV_CHAR", new Short(PREV_CHAR));
    names.put("NEWLINE", new Short(NEWLINE));
    names.put("KILL_LINE", new Short(KILL_LINE));
    names.put("PASTE", new Short(PASTE));
    names.put("CLEAR_SCREEN", new Short(CLEAR_SCREEN));
    names.put("NEXT_HISTORY", new Short(NEXT_HISTORY));
    names.put("PREV_HISTORY", new Short(PREV_HISTORY));
    names.put("START_OF_HISTORY", new Short(START_OF_HISTORY));
    names.put("END_OF_HISTORY", new Short(END_OF_HISTORY));
    names.put("REDISPLAY", new Short(REDISPLAY));
    names.put("KILL_LINE_PREV", new Short(KILL_LINE_PREV));
    names.put("DELETE_PREV_WORD", new Short(DELETE_PREV_WORD));
    names.put("NEXT_CHAR", new Short(NEXT_CHAR));
    names.put("REPEAT_PREV_CHAR", new Short(REPEAT_PREV_CHAR));
    names.put("SEARCH_PREV", new Short(SEARCH_PREV));
    names.put("REPEAT_NEXT_CHAR", new Short(REPEAT_NEXT_CHAR));
    names.put("SEARCH_NEXT", new Short(SEARCH_NEXT));
    names.put("PREV_SPACE_WORD", new Short(PREV_SPACE_WORD));
    names.put("TO_END_WORD", new Short(TO_END_WORD));
    names.put("REPEAT_SEARCH_PREV", new Short(REPEAT_SEARCH_PREV));
    names.put("PASTE_PREV", new Short(PASTE_PREV));
    names.put("REPLACE_MODE", new Short(REPLACE_MODE));
    names.put("SUBSTITUTE_LINE", new Short(SUBSTITUTE_LINE));
    names.put("TO_PREV_CHAR", new Short(TO_PREV_CHAR));
    names.put("NEXT_SPACE_WORD", new Short(NEXT_SPACE_WORD));
    names.put("DELETE_PREV_CHAR", new Short(DELETE_PREV_CHAR));
    names.put("ADD", new Short(ADD));
    names.put("PREV_WORD", new Short(PREV_WORD));
    names.put("CHANGE_META", new Short(CHANGE_META));
    names.put("DELETE_META", new Short(DELETE_META));
    names.put("END_WORD", new Short(END_WORD));
    names.put("NEXT_CHAR", new Short(NEXT_CHAR));
    names.put("INSERT", new Short(INSERT));
    names.put("REPEAT_SEARCH_NEXT", new Short(REPEAT_SEARCH_NEXT));
    names.put("PASTE_NEXT", new Short(PASTE_NEXT));
    names.put("REPLACE_CHAR", new Short(REPLACE_CHAR));
    names.put("SUBSTITUTE_CHAR", new Short(SUBSTITUTE_CHAR));
    names.put("TO_NEXT_CHAR", new Short(TO_NEXT_CHAR));
    names.put("UNDO", new Short(UNDO));
    names.put("NEXT_WORD", new Short(NEXT_WORD));
    names.put("DELETE_NEXT_CHAR", new Short(DELETE_NEXT_CHAR));
    names.put("CHANGE_CASE", new Short(CHANGE_CASE));
    names.put("COMPLETE", new Short(COMPLETE));
    names.put("EXIT", new Short(EXIT));
    names.put("CLEAR_LINE", new Short(CLEAR_LINE));

    KEYMAP_NAMES = new TreeMap(Collections.unmodifiableMap(names));
  }

  /** The map for logical operations. */
  private final short[] keybindings;

  /** If true, issue an audible keyboard bell when appropriate. */
  private boolean bellEnabled = true;

  /** The current character mask. */
  private Character mask = null;

  /** The null mask. */
  private static final Character NULL_MASK = new Character((char) 0);

  /**
   * The number of tab-completion candidates above which a warning will be prompted before showing
   * all the candidates.
   */
  private int autoprintThreshhold =
      Integer.getInteger("jline.completion.threshold", 100).intValue(); // same default as

  // bash

  /** The Terminal to use. */
  private final Terminal terminal;

  private CompletionHandler completionHandler = new CandidateListCompletionHandler();

  InputStream in;

  final Writer out;

  final CursorBuffer buf = new CursorBuffer();

  static PrintWriter debugger;

  History history = new History();

  final List completors = new LinkedList();

  private Character echoCharacter = null;

  private Map triggeredActions = new HashMap();

  /**
   * Adding a triggered Action allows to give another curse of action if a character passed the
   * preprocessing.
   *
   * <p>Say you want to close the application if the user enter q. addTriggerAction('q', new
   * ActionListener(){ System.exit(0); }); would do the trick.
   *
   * @param c
   * @param listener
   */
  public void addTriggeredAction(char c, ActionListener listener) {
    triggeredActions.put(new Character(c), listener);
  }

  /**
   * Create a new reader using {@link FileDescriptor#in} for input and {@link System#out} for
   * output. {@link FileDescriptor#in} is used because it has a better chance of being unbuffered.
   */
  public ConsoleReader() throws IOException {
    this(new FileInputStream(FileDescriptor.in), new PrintWriter(System.out));
  }

  /**
   * Create a new reader using the specified {@link InputStream} for input and the specific writer
   * for output, using the default keybindings resource.
   */
  public ConsoleReader(final InputStream in, final Writer out) throws IOException {
    this(in, out, null);
  }

  public ConsoleReader(final InputStream in, final Writer out, final InputStream bindings)
      throws IOException {
    this(in, out, bindings, Terminal.getTerminal());
  }

  /**
   * Create a new reader.
   *
   * @param in the input
   * @param out the output
   * @param bindings the key bindings to use
   * @param term the terminal to use
   */
  public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
      throws IOException {
    this.terminal = term;
    setInput(in);
    this.out = out;

    if (bindings == null) {
      try {
        String bindingFile =
            System.getProperty(
                "jline.keybindings",
                new File(System.getProperty("user.home", ".jlinebindings.properties"))
                    .getAbsolutePath());

        if (new File(bindingFile).isFile()) {
          bindings = new FileInputStream(new File(bindingFile));
        }
      } catch (Exception e) {
        // swallow exceptions with option debugging
        if (debugger != null) {
          e.printStackTrace(debugger);
        }
      }
    }

    if (bindings == null) {
      bindings = terminal.getDefaultBindings();
    }

    this.keybindings = new short[Character.MAX_VALUE * 2];

    Arrays.fill(this.keybindings, UNKNOWN);

    /**
     * Loads the key bindings. Bindings file is in the format:
     *
     * <p>keycode: operation name
     */
    if (bindings != null) {
      Properties p = new Properties();
      p.load(bindings);
      bindings.close();

      for (Iterator i = p.keySet().iterator(); i.hasNext(); ) {
        String val = (String) i.next();

        try {
          Short code = new Short(val);
          String op = (String) p.getProperty(val);

          Short opval = (Short) KEYMAP_NAMES.get(op);

          if (opval != null) {
            keybindings[code.shortValue()] = opval.shortValue();
          }
        } catch (NumberFormatException nfe) {
          consumeException(nfe);
        }
      }

      // hardwired arrow key bindings
      // keybindings[VK_UP] = PREV_HISTORY;
      // keybindings[VK_DOWN] = NEXT_HISTORY;
      // keybindings[VK_LEFT] = PREV_CHAR;
      // keybindings[VK_RIGHT] = NEXT_CHAR;
    }
  }

  public Terminal getTerminal() {
    return this.terminal;
  }

  /** Set the stream for debugging. Development use only. */
  public void setDebug(final PrintWriter debugger) {
    ConsoleReader.debugger = debugger;
  }

  /** Set the stream to be used for console input. */
  public void setInput(final InputStream in) {
    this.in = in;
  }

  /** Returns the stream used for console input. */
  public InputStream getInput() {
    return this.in;
  }

  /** Read the next line and return the contents of the buffer. */
  public String readLine() throws IOException {
    return readLine((String) null);
  }

  /**
   * Read the next line with the specified character mask. If null, then characters will be echoed.
   * If 0, then no characters will be echoed.
   */
  public String readLine(final Character mask) throws IOException {
    return readLine(null, mask);
  }

  /** @param bellEnabled if true, enable audible keyboard bells if an alert is required. */
  public void setBellEnabled(final boolean bellEnabled) {
    this.bellEnabled = bellEnabled;
  }

  /** @return true is audible keyboard bell is enabled. */
  public boolean getBellEnabled() {
    return this.bellEnabled;
  }

  /**
   * Query the terminal to find the current width;
   *
   * @see Terminal#getTerminalWidth
   * @return the width of the current terminal.
   */
  public int getTermwidth() {
    return Terminal.setupTerminal().getTerminalWidth();
  }

  /**
   * Query the terminal to find the current width;
   *
   * @see Terminal#getTerminalHeight
   * @return the height of the current terminal.
   */
  public int getTermheight() {
    return Terminal.setupTerminal().getTerminalHeight();
  }

  /** @param autoprintThreshhold the number of candidates to print without issuing a warning. */
  public void setAutoprintThreshhold(final int autoprintThreshhold) {
    this.autoprintThreshhold = autoprintThreshhold;
  }

  /** @return the number of candidates to print without issing a warning. */
  public int getAutoprintThreshhold() {
    return this.autoprintThreshhold;
  }

  int getKeyForAction(short logicalAction) {
    for (int i = 0; i < keybindings.length; i++) {
      if (keybindings[i] == logicalAction) {
        return i;
      }
    }

    return -1;
  }

  /** Clear the echoed characters for the specified character code. */
  int clearEcho(int c) throws IOException {
    // if the terminal is not echoing, then just return...
    if (!terminal.getEcho()) {
      return 0;
    }

    // otherwise, clear
    int num = countEchoCharacters((char) c);
    back(num);
    drawBuffer(num);

    return num;
  }

  int countEchoCharacters(char c) {
    // tabs as special: we need to determine the number of spaces
    // to cancel based on what out current cursor position is
    if (c == 9) {
      int tabstop = 8; // will this ever be different?
      int position = getCursorPosition();

      return tabstop - (position % tabstop);
    }

    return getPrintableCharacters(c).length();
  }

  /**
   * Return the number of characters that will be printed when the specified character is echoed to
   * the screen. Adapted from cat by Torbjorn Granlund, as repeated in stty by David MacKenzie.
   */
  StringBuffer getPrintableCharacters(char ch) {
    StringBuffer sbuff = new StringBuffer();

    if (ch >= 32) {
      if (ch < 127) {
        sbuff.append(ch);
      } else if (ch == 127) {
        sbuff.append('^');
        sbuff.append('?');
      } else {
        sbuff.append('M');
        sbuff.append('-');

        if (ch >= (128 + 32)) {
          if (ch < (128 + 127)) {
            sbuff.append((char) (ch - 128));
          } else {
            sbuff.append('^');
            sbuff.append('?');
          }
        } else {
          sbuff.append('^');
          sbuff.append((char) (ch - 128 + 64));
        }
      }
    } else {
      sbuff.append('^');
      sbuff.append((char) (ch + 64));
    }

    return sbuff;
  }

  int getCursorPosition() {
    // FIXME: does not handle anything but a line with a prompt
    // absolute position
    return ((prompt == null) ? 0 : prompt.length()) + buf.cursor;
  }

  public String readLine(final String prompt) throws IOException {
    return readLine(prompt, null);
  }

  /** The default prompt that will be issued. */
  public void setDefaultPrompt(String prompt) {
    this.prompt = prompt;
  }

  /** The default prompt that will be issued. */
  public String getDefaultPrompt() {
    return prompt;
  }

  /**
   * Read a line from the <i>in</i> {@link InputStream}, and return the line (without any trailing
   * newlines).
   *
   * @param prompt the prompt to issue to the console, may be null.
   * @return a line that is read from the terminal, or null if there was null input (e.g.,
   *     <i>CTRL-D</i> was pressed).
   */
  public String readLine(final String prompt, final Character mask) throws IOException {
    this.mask = mask;
    if (prompt != null) this.prompt = prompt;

    try {
      terminal.beforeReadLine(this, this.prompt, mask);

      if ((this.prompt != null) && (this.prompt.length() > 0)) {
        out.write(this.prompt);
        out.flush();
      }

      // if the terminal is unsupported, just use plain-java reading
      if (!terminal.isSupported()) {
        return readLine(in);
      }

      while (true) {
        int[] next = readBinding();

        if (next == null) {
          return null;
        }

        int c = next[0];
        int code = next[1];

        if (c == -1) {
          return null;
        }

        boolean success = true;

        switch (code) {
          case EXIT: // ctrl-d
            if (buf.buffer.length() == 0) {
              return null;
            }

          case COMPLETE: // tab
            success = complete();
            break;

          case MOVE_TO_BEG:
            success = setCursorPosition(0);
            break;

          case KILL_LINE: // CTRL-K
            success = killLine();
            break;

          case CLEAR_SCREEN: // CTRL-L
            success = clearScreen();
            break;

          case KILL_LINE_PREV: // CTRL-U
            success = resetLine();
            break;

          case NEWLINE: // enter
            moveToEnd();
            printNewline(); // output newline
            return finishBuffer();

          case DELETE_PREV_CHAR: // backspace
            success = backspace();
            break;

          case DELETE_NEXT_CHAR: // delete
            success = deleteCurrentCharacter();
            break;

          case MOVE_TO_END:
            success = moveToEnd();
            break;

          case PREV_CHAR:
            success = moveCursor(-1) != 0;
            break;

          case NEXT_CHAR:
            success = moveCursor(1) != 0;
            break;

          case NEXT_HISTORY:
            success = moveHistory(true);
            break;

          case PREV_HISTORY:
            success = moveHistory(false);
            break;

          case REDISPLAY:
            break;

          case PASTE:
            success = paste();
            break;

          case DELETE_PREV_WORD:
            success = deletePreviousWord();
            break;

          case PREV_WORD:
            success = previousWord();
            break;

          case NEXT_WORD:
            success = nextWord();
            break;

          case START_OF_HISTORY:
            success = history.moveToFirstEntry();
            if (success) setBuffer(history.current());
            break;

          case END_OF_HISTORY:
            success = history.moveToLastEntry();
            if (success) setBuffer(history.current());
            break;

          case CLEAR_LINE:
            moveInternal(-(buf.buffer.length()));
            killLine();
            break;

          case INSERT:
            buf.setOvertyping(!buf.isOvertyping());
            break;

          case UNKNOWN:
          default:
            if (c != 0) { // ignore null chars
              ActionListener action =
                  (ActionListener) triggeredActions.get(new Character((char) c));
              if (action != null) action.actionPerformed(null);
              else putChar(c, true);
            } else success = false;
        }

        if (!(success)) {
          beep();
        }

        flushConsole();
      }
    } finally {
      terminal.afterReadLine(this, this.prompt, mask);
    }
  }

  private String readLine(InputStream in) throws IOException {
    StringBuffer buf = new StringBuffer();

    while (true) {
      int i = in.read();

      if ((i == -1) || (i == '\n') || (i == '\r')) {
        return buf.toString();
      }

      buf.append((char) i);
    }

    // return new BufferedReader (new InputStreamReader (in)).readLine ();
  }

  /** Reads the console input and returns an array of the form [raw, key binding]. */
  private int[] readBinding() throws IOException {
    int c = readVirtualKey();

    if (c == -1) {
      return null;
    }

    // extract the appropriate key binding
    short code = keybindings[c];

    if (debugger != null) {
      debug("    translated: " + (int) c + ": " + code);
    }

    return new int[] {c, code};
  }

  /**
   * Move up or down the history tree.
   *
   * @param direction less than 0 to move up the tree, down otherwise
   */
  private final boolean moveHistory(final boolean next) throws IOException {
    if (next && !history.next()) {
      return false;
    } else if (!next && !history.previous()) {
      return false;
    }

    setBuffer(history.current());

    return true;
  }

  /**
   * Paste the contents of the clipboard into the console buffer
   *
   * @return true if clipboard contents pasted
   */
  public boolean paste() throws IOException {
    Clipboard clipboard;
    try { // May throw ugly exception on system without X
      clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    } catch (Exception e) {
      return false;
    }

    if (clipboard == null) {
      return false;
    }

    Transferable transferable = clipboard.getContents(null);

    if (transferable == null) {
      return false;
    }

    try {
      Object content = transferable.getTransferData(DataFlavor.plainTextFlavor);

      /*
       * This fix was suggested in bug #1060649 at
       * http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056
       * to get around the deprecated DataFlavor.plainTextFlavor, but it
       * raises a UnsupportedFlavorException on Mac OS X
       */
      if (content == null) {
        try {
          content = new DataFlavor().getReaderForText(transferable);
        } catch (Exception e) {
        }
      }

      if (content == null) {
        return false;
      }

      String value;

      if (content instanceof Reader) {
        // TODO: we might want instead connect to the input stream
        // so we can interpret individual lines
        value = "";

        String line = null;

        for (BufferedReader read = new BufferedReader((Reader) content);
            (line = read.readLine()) != null; ) {
          if (value.length() > 0) {
            value += "\n";
          }

          value += line;
        }
      } else {
        value = content.toString();
      }

      if (value == null) {
        return true;
      }

      putString(value);

      return true;
    } catch (UnsupportedFlavorException ufe) {
      if (debugger != null) debug(ufe + "");

      return false;
    }
  }

  /**
   * Kill the buffer ahead of the current cursor position.
   *
   * @return true if successful
   */
  public boolean killLine() throws IOException {
    int cp = buf.cursor;
    int len = buf.buffer.length();

    if (cp >= len) {
      return false;
    }

    int num = buf.buffer.length() - cp;
    clearAhead(num);

    for (int i = 0; i < num; i++) {
      buf.buffer.deleteCharAt(len - i - 1);
    }

    return true;
  }

  /** Clear the screen by issuing the ANSI "clear screen" code. */
  public boolean clearScreen() throws IOException {
    if (!terminal.isANSISupported()) {
      return false;
    }

    // send the ANSI code to clear the screen
    printString(((char) 27) + "[2J");
    flushConsole();

    // then send the ANSI code to go to position 1,1
    printString(((char) 27) + "[1;1H");
    flushConsole();

    redrawLine();

    return true;
  }

  /**
   * Use the completors to modify the buffer with the appropriate completions.
   *
   * @return true if successful
   */
  private final boolean complete() throws IOException {
    // debug ("tab for (" + buf + ")");
    if (completors.size() == 0) {
      return false;
    }

    List candidates = new LinkedList();
    String bufstr = buf.buffer.toString();
    int cursor = buf.cursor;

    int position = -1;

    for (Iterator i = completors.iterator(); i.hasNext(); ) {
      Completor comp = (Completor) i.next();

      if ((position = comp.complete(bufstr, cursor, candidates)) != -1) {
        break;
      }
    }

    // no candidates? Fail.
    if (candidates.size() == 0) {
      return false;
    }

    return completionHandler.complete(this, candidates, position);
  }

  public CursorBuffer getCursorBuffer() {
    return buf;
  }

  /**
   * Output the specified {@link Collection} in proper columns.
   *
   * @param stuff the stuff to print
   */
  public void printColumns(final Collection stuff) throws IOException {
    if ((stuff == null) || (stuff.size() == 0)) {
      return;
    }

    int width = getTermwidth();
    int maxwidth = 0;

    for (Iterator i = stuff.iterator();
        i.hasNext();
        maxwidth = Math.max(maxwidth, i.next().toString().length())) {;
    }

    StringBuffer line = new StringBuffer();

    int showLines;

    if (usePagination) showLines = getTermheight() - 1; // page limit
    else showLines = Integer.MAX_VALUE;

    for (Iterator i = stuff.iterator(); i.hasNext(); ) {
      String cur = (String) i.next();

      if ((line.length() + maxwidth) > width) {
        printString(line.toString().trim());
        printNewline();
        line.setLength(0);
        if (--showLines == 0) { // Overflow
          printString(loc.getString("display-more"));
          flushConsole();
          int c = readVirtualKey();
          if (c == '\r' || c == '\n') showLines = 1; // one step forward
          else if (c != 'q') showLines = getTermheight() - 1; // page forward

          back(loc.getString("display-more").length());
          if (c == 'q') break; // cancel
        }
      }

      pad(cur, maxwidth + 3, line);
    }

    if (line.length() > 0) {
      printString(line.toString().trim());
      printNewline();
      line.setLength(0);
    }
  }

  /**
   * Append <i>toPad</i> to the specified <i>appendTo</i>, as well as (<i>toPad.length () - len</i>)
   * spaces.
   *
   * @param toPad the {@link String} to pad
   * @param len the target length
   * @param appendTo the {@link StringBuffer} to which to append the padded {@link String}.
   */
  private final void pad(final String toPad, final int len, final StringBuffer appendTo) {
    appendTo.append(toPad);

    for (int i = 0; i < (len - toPad.length()); i++, appendTo.append(' ')) {;
    }
  }

  /**
   * Add the specified {@link Completor} to the list of handlers for tab-completion.
   *
   * @param completor the {@link Completor} to add
   * @return true if it was successfully added
   */
  public boolean addCompletor(final Completor completor) {
    return completors.add(completor);
  }

  /**
   * Remove the specified {@link Completor} from the list of handlers for tab-completion.
   *
   * @param completor the {@link Completor} to remove
   * @return true if it was successfully removed
   */
  public boolean removeCompletor(final Completor completor) {
    return completors.remove(completor);
  }

  /** Returns an unmodifiable list of all the completors. */
  public Collection getCompletors() {
    return Collections.unmodifiableList(completors);
  }

  /**
   * Erase the current line.
   *
   * @return false if we failed (e.g., the buffer was empty)
   */
  final boolean resetLine() throws IOException {
    if (buf.cursor == 0) {
      return false;
    }

    backspaceAll();

    return true;
  }

  /** Move the cursor position to the specified absolute index. */
  public final boolean setCursorPosition(final int position) throws IOException {
    return moveCursor(position - buf.cursor) != 0;
  }

  /**
   * Set the current buffer's content to the specified {@link String}. The visual console will be
   * modified to show the current buffer.
   *
   * @param buffer the new contents of the buffer.
   */
  private final void setBuffer(final String buffer) throws IOException {
    // don't bother modifying it if it is unchanged
    if (buffer.equals(buf.buffer.toString())) {
      return;
    }

    // obtain the difference between the current buffer and the new one
    int sameIndex = 0;

    for (int i = 0, l1 = buffer.length(), l2 = buf.buffer.length(); (i < l1) && (i < l2); i++) {
      if (buffer.charAt(i) == buf.buffer.charAt(i)) {
        sameIndex++;
      } else {
        break;
      }
    }

    int diff = buf.buffer.length() - sameIndex;

    backspace(diff); // go back for the differences
    killLine(); // clear to the end of the line
    buf.buffer.setLength(sameIndex); // the new length
    putString(buffer.substring(sameIndex)); // append the differences
  }

  /** Clear the line and redraw it. */
  public final void redrawLine() throws IOException {
    printCharacter(RESET_LINE);
    flushConsole();
    drawLine();
  }

  /** Output put the prompt + the current buffer */
  public final void drawLine() throws IOException {
    if (prompt != null) {
      printString(prompt);
    }

    printString(buf.buffer.toString());

    if (buf.length() != buf.cursor) // not at end of line
    back(buf.length() - buf.cursor); // sync
  }

  /** Output a platform-dependant newline. */
  public final void printNewline() throws IOException {
    printString(CR);
    flushConsole();
  }

  /**
   * Clear the buffer and add its contents to the history.
   *
   * @return the former contents of the buffer.
   */
  final String finishBuffer() {
    String str = buf.buffer.toString();

    // we only add it to the history if the buffer is not empty
    // and if mask is null, since having a mask typically means
    // the string was a password. We clear the mask after this call
    if (str.length() > 0) {
      if (mask == null && useHistory) {
        history.addToHistory(str);
      } else {
        mask = null;
      }
    }

    history.moveToEnd();

    buf.buffer.setLength(0);
    buf.cursor = 0;

    return str;
  }

  /** Write out the specified string to the buffer and the output stream. */
  public final void putString(final String str) throws IOException {
    buf.write(str);
    printString(str);
    drawBuffer();
  }

  /** Output the specified string to the output stream (but not the buffer). */
  public final void printString(final String str) throws IOException {
    printCharacters(str.toCharArray());
  }

  /** Output the specified character, both to the buffer and the output stream. */
  private final void putChar(final int c, final boolean print) throws IOException {
    buf.write((char) c);

    if (print) {
      // no masking...
      if (mask == null) {
        printCharacter(c);
      }
      // null mask: don't print anything...
      else if (mask.charValue() == 0) {;
      }
      // otherwise print the mask...
      else {
        printCharacter(mask.charValue());
      }

      drawBuffer();
    }
  }

  /**
   * Redraw the rest of the buffer from the cursor onwards. This is necessary for inserting text
   * into the buffer.
   *
   * @param clear the number of characters to clear after the end of the buffer
   */
  private final void drawBuffer(final int clear) throws IOException {
    // debug ("drawBuffer: " + clear);
    char[] chars = buf.buffer.substring(buf.cursor).toCharArray();
    if (mask != null) Arrays.fill(chars, mask.charValue());

    printCharacters(chars);

    clearAhead(clear);
    back(chars.length);
    flushConsole();
  }

  /**
   * Redraw the rest of the buffer from the cursor onwards. This is necessary for inserting text
   * into the buffer.
   */
  private final void drawBuffer() throws IOException {
    drawBuffer(0);
  }

  /** Clear ahead the specified number of characters without moving the cursor. */
  private final void clearAhead(final int num) throws IOException {
    if (num == 0) {
      return;
    }

    // debug ("clearAhead: " + num);

    // print blank extra characters
    printCharacters(' ', num);

    // we need to flush here so a "clever" console
    // doesn't just ignore the redundancy of a space followed by
    // a backspace.
    flushConsole();

    // reset the visual cursor
    back(num);

    flushConsole();
  }

  /** Move the visual cursor backwards without modifying the buffer cursor. */
  private final void back(final int num) throws IOException {
    printCharacters(BACKSPACE, num);
    flushConsole();
  }

  /** Issue an audible keyboard bell, if {@link #getBellEnabled} return true. */
  public final void beep() throws IOException {
    if (!(getBellEnabled())) {
      return;
    }

    printCharacter(KEYBOARD_BELL);
    // need to flush so the console actually beeps
    flushConsole();
  }

  /**
   * Output the specified character to the output stream without manipulating the current buffer.
   */
  private final void printCharacter(final int c) throws IOException {
    if (c == '\t') {
      char cbuf[] = new char[TAB_WIDTH];
      Arrays.fill(cbuf, ' ');
      out.write(cbuf);
      return;
    }

    out.write(c);
  }

  /**
   * Output the specified characters to the output stream without manipulating the current buffer.
   */
  private final void printCharacters(final char[] c) throws IOException {
    int len = 0;
    for (int i = 0; i < c.length; i++)
      if (c[i] == '\t') len += TAB_WIDTH;
      else len++;

    char cbuf[];
    if (len == c.length) cbuf = c;
    else {
      cbuf = new char[len];
      int pos = 0;
      for (int i = 0; i < c.length; i++) {
        if (c[i] == '\t') {
          Arrays.fill(cbuf, pos, pos + TAB_WIDTH, ' ');
          pos += TAB_WIDTH;
        } else {
          cbuf[pos] = c[i];
          pos++;
        }
      }
    }

    out.write(cbuf);
  }

  private final void printCharacters(final char c, final int num) throws IOException {
    if (num == 1) {
      printCharacter(c);
    } else {
      char[] chars = new char[num];
      Arrays.fill(chars, c);
      printCharacters(chars);
    }
  }

  /**
   * Flush the console output stream. This is important for printout out single characters (like a
   * backspace or keyboard) that we want the console to handle immedately.
   */
  public final void flushConsole() throws IOException {
    out.flush();
  }

  private final int backspaceAll() throws IOException {
    return backspace(Integer.MAX_VALUE);
  }

  /**
   * Issue <em>num</em> backspaces.
   *
   * @return the number of characters backed up
   */
  private final int backspace(final int num) throws IOException {
    if (buf.cursor == 0) {
      return 0;
    }

    int count = 0;

    count = moveCursor(-1 * num) * -1;
    // debug ("Deleting from " + buf.cursor + " for " + count);
    buf.buffer.delete(buf.cursor, buf.cursor + count);
    drawBuffer(count);

    return count;
  }

  /**
   * Issue a backspace.
   *
   * @return true if successful
   */
  public final boolean backspace() throws IOException {
    return backspace(1) == 1;
  }

  private final boolean moveToEnd() throws IOException {
    if (moveCursor(1) == 0) {
      return false;
    }

    while (moveCursor(1) != 0) {;
    }

    return true;
  }

  /** Delete the character at the current position and redraw the remainder of the buffer. */
  private final boolean deleteCurrentCharacter() throws IOException {
    boolean success = buf.buffer.length() > 0;
    if (!success) {
      return false;
    }

    if (buf.cursor == buf.buffer.length()) {
      return false;
    }

    buf.buffer.deleteCharAt(buf.cursor);
    drawBuffer(1);
    return true;
  }

  private final boolean previousWord() throws IOException {
    while (isDelimiter(buf.current()) && (moveCursor(-1) != 0)) {;
    }

    while (!isDelimiter(buf.current()) && (moveCursor(-1) != 0)) {;
    }

    return true;
  }

  private final boolean nextWord() throws IOException {
    while (isDelimiter(buf.current()) && (moveCursor(1) != 0)) {;
    }

    while (!isDelimiter(buf.current()) && (moveCursor(1) != 0)) {;
    }

    return true;
  }

  private final boolean deletePreviousWord() throws IOException {
    while (isDelimiter(buf.current()) && backspace()) {;
    }

    while (!isDelimiter(buf.current()) && backspace()) {;
    }

    return true;
  }

  /**
   * Move the cursor <i>where</i> characters.
   *
   * @param where if less than 0, move abs(<i>where</i>) to the left, otherwise move <i>where</i> to
   *     the right.
   * @return the number of spaces we moved
   */
  public final int moveCursor(final int num) throws IOException {
    int where = num;

    if ((buf.cursor == 0) && (where < 0)) {
      return 0;
    }

    if ((buf.cursor == buf.buffer.length()) && (where > 0)) {
      return 0;
    }

    if ((buf.cursor + where) < 0) {
      where = -buf.cursor;
    } else if ((buf.cursor + where) > buf.buffer.length()) {
      where = buf.buffer.length() - buf.cursor;
    }

    moveInternal(where);

    return where;
  }

  /**
   * debug.
   *
   * @param str the message to issue.
   */
  public static void debug(final String str) {
    if (debugger != null) {
      debugger.println(str);
      debugger.flush();
    }
  }

  /**
   * Move the cursor <i>where</i> characters, withough checking the current buffer.
   *
   * @see #where
   * @param where the number of characters to move to the right or left.
   */
  private final void moveInternal(final int where) throws IOException {
    // debug ("move cursor " + where + " ("
    // + buf.cursor + " => " + (buf.cursor + where) + ")");
    buf.cursor += where;

    char c;

    if (where < 0) {
      int len = 0;
      for (int i = buf.cursor; i < buf.cursor - where; i++) {
        if (buf.getBuffer().charAt(i) == '\t') len += TAB_WIDTH;
        else len++;
      }

      char cbuf[] = new char[len];
      Arrays.fill(cbuf, BACKSPACE);
      out.write(cbuf);

      return;
    } else if (buf.cursor == 0) {
      return;
    } else if (mask != null) {
      c = mask.charValue();
    } else {
      printCharacters(buf.buffer.substring(buf.cursor - where, buf.cursor).toCharArray());
      return;
    }

    // null character mask: don't output anything
    if (NULL_MASK.equals(mask)) {
      return;
    }

    printCharacters(c, Math.abs(where));
  }

  /**
   * Read a character from the console.
   *
   * @return the character, or -1 if an EOF is received.
   */
  public final int readVirtualKey() throws IOException {
    int c = terminal.readVirtualKey(in);

    if (debugger != null) {
      debug("keystroke: " + c + "");
    }

    // clear any echo characters
    clearEcho(c);

    return c;
  }

  public final int readCharacter(final char[] allowed) throws IOException {
    // if we restrict to a limited set and the current character
    // is not in the set, then try again.
    char c;

    Arrays.sort(allowed); // always need to sort before binarySearch

    while (Arrays.binarySearch(allowed, c = (char) readVirtualKey()) < 0) ;

    return c;
  }

  /**
   * Issue <em>num</em> deletes.
   *
   * @return the number of characters backed up
   */
  private final int delete(final int num) throws IOException {
    /* Commented out beacuse of DWA-2949:
    if (buf.cursor == 0)
                return 0;*/

    buf.buffer.delete(buf.cursor, buf.cursor + 1);
    drawBuffer(1);

    return 1;
  }

  public final boolean replace(int num, String replacement) {
    buf.buffer.replace(buf.cursor - num, buf.cursor, replacement);
    try {
      moveCursor(-num);
      drawBuffer(Math.max(0, num - replacement.length()));
      moveCursor(replacement.length());
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * Issue a delete.
   *
   * @return true if successful
   */
  public final boolean delete() throws IOException {
    return delete(1) == 1;
  }

  public void setHistory(final History history) {
    this.history = history;
  }

  public History getHistory() {
    return this.history;
  }

  public void setCompletionHandler(final CompletionHandler completionHandler) {
    this.completionHandler = completionHandler;
  }

  public CompletionHandler getCompletionHandler() {
    return this.completionHandler;
  }

  /**
   * Set the echo character. For example, to have "*" entered when a password is typed:
   *
   * <pre>
   * myConsoleReader.setEchoCharacter(new Character('*'));
   * </pre>
   *
   * <p>Setting the character to
   *
   * <pre>
   * null
   * </pre>
   *
   * will restore normal character echoing. Setting the character to
   *
   * <pre>
   * new Character(0)
   * </pre>
   *
   * will cause nothing to be echoed.
   *
   * @param echoCharacter the character to echo to the console in place of the typed character.
   */
  public void setEchoCharacter(final Character echoCharacter) {
    this.echoCharacter = echoCharacter;
  }

  /** Returns the echo character. */
  public Character getEchoCharacter() {
    return this.echoCharacter;
  }

  /** No-op for exceptions we want to silently consume. */
  private void consumeException(final Throwable e) {}

  /**
   * Checks to see if the specified character is a delimiter. We consider a character a delimiter if
   * it is anything but a letter or digit.
   *
   * @param c the character to test
   * @return true if it is a delimiter
   */
  private boolean isDelimiter(char c) {
    return !Character.isLetterOrDigit(c);
  }

  /** Whether or not to add new commands to the history buffer. */
  public void setUseHistory(boolean useHistory) {
    this.useHistory = useHistory;
  }

  /** Whether or not to add new commands to the history buffer. */
  public boolean getUseHistory() {
    return useHistory;
  }

  /**
   * Whether to use pagination when the number of rows of candidates exceeds the height of the
   * temrinal.
   */
  public void setUsePagination(boolean usePagination) {
    this.usePagination = usePagination;
  }

  /**
   * Whether to use pagination when the number of rows of candidates exceeds the height of the
   * temrinal.
   */
  public boolean getUsePagination() {
    return this.usePagination;
  }
}
  /**
   * Called by constructors to initialize the dialog.
   *
   * @since ostermillerutils 1.00.00
   */
  @Override
  protected void dialogInit() {

    if (labels == null) {
      setLocale(Locale.getDefault());
    }

    name = new JTextField("", 20);
    pass = new JPasswordField("", 20);
    okButton = new JButton(labels.getString("dialog.ok"));
    cancelButton = new JButton(labels.getString("dialog.cancel"));
    nameLabel = new JLabel(labels.getString("dialog.name") + " ");
    passLabel = new JLabel(labels.getString("dialog.pass") + " ");

    super.dialogInit();

    KeyListener keyListener =
        (new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE
                || (e.getSource() == cancelButton && e.getKeyCode() == KeyEvent.VK_ENTER)) {
              pressed_OK = false;
              PasswordDialog.this.setVisible(false);
            }
            if (e.getSource() == okButton && e.getKeyCode() == KeyEvent.VK_ENTER) {
              pressed_OK = true;
              PasswordDialog.this.setVisible(false);
            }
          }
        });
    addKeyListener(keyListener);

    ActionListener actionListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source == name) {
              // the user pressed enter in the name field.
              name.transferFocus();
            } else {
              // other actions close the dialog.
              pressed_OK = (source == pass || source == okButton);
              PasswordDialog.this.setVisible(false);
            }
          }
        };

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    c.insets.top = 5;
    c.insets.bottom = 5;
    JPanel pane = new JPanel(gridbag);
    pane.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20));
    c.anchor = GridBagConstraints.EAST;
    gridbag.setConstraints(nameLabel, c);
    pane.add(nameLabel);

    gridbag.setConstraints(name, c);
    name.addActionListener(actionListener);
    name.addKeyListener(keyListener);
    pane.add(name);

    c.gridy = 1;
    gridbag.setConstraints(passLabel, c);
    pane.add(passLabel);

    gridbag.setConstraints(pass, c);
    pass.addActionListener(actionListener);
    pass.addKeyListener(keyListener);
    pane.add(pass);

    c.gridy = 2;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.anchor = GridBagConstraints.CENTER;
    JPanel panel = new JPanel();
    okButton.addActionListener(actionListener);
    okButton.addKeyListener(keyListener);
    panel.add(okButton);
    cancelButton.addActionListener(actionListener);
    cancelButton.addKeyListener(keyListener);
    panel.add(cancelButton);
    gridbag.setConstraints(panel, c);
    pane.add(panel);

    getContentPane().add(pane);

    pack();
  }
예제 #27
0
  // ------------------------------------------------------------------------
  TextViewer(JFrame inParentFrame) {
    // super(true); //is double buffered - only for panels

    textViewerFrame = this;
    parentFrame = inParentFrame;
    lastViewedDirStr = "";
    lastViewedFileStr = "";

    setTitle(resources.getString("Title"));
    addWindowListener(new AppCloser());
    pack();
    setSize(500, 600);

    warningPopup = new WarningDialog(this);
    okCancelPopup = new WarningDialogOkCancel(this);
    messagePopup = new MessageDialog(this);
    okCancelMessagePopup = new MessageDialogOkCancel(this);

    // Force SwingSet to come up in the Cross Platform L&F
    try {
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      // If you want the System L&F instead, comment out the above line and
      // uncomment the following:
      // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception exc) {
      String errstr = "TextViewer:Error loading L&F: " + exc;
      warningPopup.display(errstr);
    }

    Container cf = getContentPane();
    cf.setBackground(Color.lightGray);
    // Border etched=BorderFactory.createEtchedBorder();
    // Border title=BorderFactory.createTitledBorder(etched,"TextViewer");
    // cf.setBorder(title);
    cf.setLayout(new BorderLayout());

    // create the embedded JTextComponent
    editor1 = createEditor();
    editor1.setFont(new Font("monospaced", Font.PLAIN, 12));
    // aa -added next line
    setPlainDocument((PlainDocument) editor1.getDocument()); // sets doc1

    // install the command table
    commands = new Hashtable();
    Action[] actions = getActions();
    for (int i = 0; i < actions.length; i++) {
      Action a = actions[i];
      commands.put(a.getValue(Action.NAME), a);
      // System.out.println("Debug:TextViewer: actionName:"+a.getValue(Action.NAME));
    }
    // editor1.setPreferredSize(new Dimension(,));
    // get setting from user preferences
    if (UserPref.keymapType.equals("Word")) {
      editor1 = updateKeymapForWord(editor1);
    } else {
      editor1 = updateKeymapForEmacs(editor1);
    }

    scroller1 = new JScrollPane();
    viewport1 = scroller1.getViewport();
    viewport1.add(editor1);
    scroller1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroller1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    try {
      String vpFlag = resources.getString("ViewportBackingStore");
      Boolean bs = new Boolean(vpFlag);
      viewport1.setBackingStoreEnabled(bs.booleanValue());
    } catch (MissingResourceException mre) {
      System.err.println("TextViewer:missing resource:" + mre.getMessage());
      // just use the viewport1 default
    }

    menuItems = new Hashtable();

    menubar = createMenubar();

    lowerPanel = new JPanel(true); // moved double buffering to here
    lowerPanel.setLayout(new BorderLayout());
    lowerPanel.add("North", createToolbar());
    lowerPanel.add("Center", scroller1);

    cf.add("North", menubar);
    cf.add("Center", lowerPanel);
    cf.add("South", createStatusbar());

    // for the find/search utilities
    mySearchDialog = new SearchDialog(this);

    // System.out.println("Debug:TextViewer: end of TextViewer constructor");

  }
/**
 * Implementaion of Framebuffer for MeTA Studio. Majorly a copy of <code>
 * org.jrman.ui.FramebufferImpl</code>.
 *
 * @author V.Ganesh
 * @version 2.0 (Part of MeTA v2.0)
 */
public class IDEJRManFramebufferImpl extends JInternalFrame implements Framebuffer {

  private JImageViewerPanel imagePanel = new JImageViewerPanel();
  private ImageViewerPanelSaveAction save =
      new ImageViewerPanelSaveAction(imagePanel, BufferedImage.TYPE_INT_ARGB);
  private String name;

  /** Creates a new instance of IDEJRManFramebufferImpl */
  public IDEJRManFramebufferImpl(String name, BufferedImage image) {
    super("JRMan rendered: " + name, true, true, true, true);
    this.name = name;

    save.setEnabled(false);

    imagePanel.setImage(image);
    imagePanel.addToolbarAction(save);
    if (image.getType() == BufferedImage.TYPE_INT_ARGB
        || image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) {
      imagePanel.setShowTransparencyPattern(true);
    }

    getRootPane().setDoubleBuffered(false);
    getContentPane().add(imagePanel);
    pack();

    ImageResource images = ImageResource.getInstance();

    // set the frame icon
    setFrameIcon(images.getJrMan());

    // add this to the IDE desktop
    MainMenuEventHandlers.getInstance(null)
        .getIdeInstance()
        .getWorkspaceDesktop()
        .addInternalFrame(this, true);
  }

  /**
   * Signal that a certain rectangular region has changed
   *
   * @param x top-left x coordinate
   * @param y top-left y coordinate
   * @param w rectangle width
   * @param h rectangle height
   */
  @Override
  public void refresh(int x, int y, int w, int h) {
    imagePanel.repaintImage(x, y, w, h);
  }

  /** Signal image is completed */
  @Override
  public void completed() {
    save.setEnabled(true);
  }

  // the resource bundels
  private static final ResourceBundle messagesBundle =
      ResourceBundle.getBundle(
          net.falappa.imageio.ImageViewerPanelSaveAction.class.getPackage().getName()
              + ".res.ImageViewerPanelActions");

  /** inner class to handle save action */
  public class ImageViewerPanelSaveAction extends AbstractAction {
    private JImageViewerPanel viewerPanel;
    private int imageType;
    private IDEFileChooser fc;

    /**
     * Constructs and initializes this object
     *
     * @param viewerPanel the <code>JImageViewerPanel</code> this action is linked to
     * @param imageType
     */
    public ImageViewerPanelSaveAction(JImageViewerPanel viewerPanel, int imageType) {
      super(messagesBundle.getString("ImageViewerPanelSaveAction.Save_1")); // $NON-NLS-1$
      assert viewerPanel != null;
      this.imageType = imageType;
      this.viewerPanel = viewerPanel;
      putValue(
          SHORT_DESCRIPTION,
          messagesBundle.getString("ImageViewerPanelSaveAction.Save_image_to_file_2"));
      //$NON-NLS-1$
      putValue(SMALL_ICON, UIManager.getIcon("FileView.floppyDriveIcon")); // $NON-NLS-1$
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      if (fc == null) {
        fc = new IDEFileChooser();
        fc.setFileView(new IDEFileView());
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(false);
        fc.setDialogTitle(
            messagesBundle.getString("ImageViewerPanelSaveAction.Choose_filename_to_save_4"));

        //$NON-NLS-1$

        // prepare file filters
        IIORegistry theRegistry = IIORegistry.getDefaultInstance();
        Iterator it = theRegistry.getServiceProviders(ImageWriterSpi.class, false);
        while (it.hasNext()) {
          ImageWriterSpi writer = (ImageWriterSpi) it.next();
          if ((imageType == BufferedImage.TYPE_INT_ARGB
                  || imageType == BufferedImage.TYPE_INT_ARGB_PRE)
              && "JPEG".equals(writer.getFormatNames()[0].toUpperCase())) continue;
          ImageWriterSpiFileFilter ff = new ImageWriterSpiFileFilter(writer);
          fc.addChoosableFileFilter(ff);
        }
      }

      if (fc.showSaveDialog(viewerPanel) == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fc.getSelectedFile();

        if (selectedFile != null) {
          String fileName = selectedFile.getAbsolutePath();
          ImageWriterSpiFileFilter ff = (ImageWriterSpiFileFilter) fc.getFileFilter();
          if (!ff.hasCorrectSuffix(fileName)) fileName = ff.addSuffix(fileName);
          selectedFile = new File(fileName);
          if (selectedFile.exists()) {
            String message =
                MessageFormat.format(
                    messagesBundle.getString("ImageViewerPanelSaveAction.Overwrite_question_5"),
                    //$NON-NLS-1$
                    fileName);
            if (JOptionPane.NO_OPTION
                == JOptionPane.showConfirmDialog(
                    viewerPanel,
                    message,
                    messagesBundle.getString("ImageViewerPanelSaveAction.Warning_6"),
                    //$NON-NLS-1$
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE)) return;
          }
          writeToFile(selectedFile, ff);
        }
      }
    }

    private void writeToFile(File selectedFile, ImageWriterSpiFileFilter ff) {
      try {
        ImageOutputStream ios = ImageIO.createImageOutputStream(selectedFile);
        ImageWriter iw = ff.getImageWriterSpi().createWriterInstance();
        iw.setOutput(ios);
        ImageWriteParam iwp = iw.getDefaultWriteParam();
        if (iwp.canWriteCompressed()) {
          iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          // set maximum image quality
          iwp.setCompressionQuality(1.f);
        }
        Image image = viewerPanel.getImage();
        BufferedImage bufferedImage;
        if (viewerPanel.getImage() instanceof BufferedImage)
          bufferedImage = (BufferedImage) viewerPanel.getImage();
        else {
          bufferedImage =
              new BufferedImage(
                  image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
          bufferedImage.createGraphics().drawImage(image, 0, 0, null);
        }
        iw.write(null, new IIOImage(bufferedImage, null, null), iwp);
        iw.dispose();
        ios.close();
      } catch (IOException ioe) {
        JOptionPane.showMessageDialog(
            viewerPanel,
            messagesBundle.getString(
                "ImageViewerPanelSaveAction." + "Error_during_image_saving_message_7"),
            //$NON-NLS-1$
            messagesBundle.getString("ImageViewerPanelSaveAction." + "Error_dialog_title_8"),
            //$NON-NLS-1$
            JOptionPane.ERROR_MESSAGE);
        ioe.printStackTrace();
      }
    }
  }
} // end of class IDEJRManFramebufferImpl
예제 #29
0
 /*==========================================================
  * constructors
  *==========================================================*/
 public DefaultLogParser() {
   mResource = ResourceBundle.getBundle(CMSAdminResources.class.getName());
 }
예제 #30
0
  /** Install the Rotate-Button into the toolbar */
  private void installRotateButton() {
    URL imgURL = ClassLoader.getSystemResource("ch/tbe/pics/rotate.gif");
    ImageIcon rotateIcon = new ImageIcon(imgURL);
    rotate = new JButton(rotateIcon);
    rotate.setEnabled(false);
    rotatePanel = new JToolBar();
    rotatePanel.setOrientation(1);
    rotatePanel.setLayout(new BorderLayout(0, 1));
    rotateSlider = new JSlider();
    rotateSlider.setMaximum(359);
    rotateSlider.setMinimum(0);
    rotateSlider.setMaximumSize(new Dimension(100, 100));
    rotateSlider.setOrientation(1);
    Box box = Box.createVerticalBox();

    sliderValue.setPreferredSize(new Dimension(30, 20));

    rotateSlider.setAlignmentY(Component.TOP_ALIGNMENT);
    box.add(sliderValue);
    box.add(rotateSlider);
    sliderValue.setAlignmentY(Component.TOP_ALIGNMENT);
    rotatePanel.add(box, BorderLayout.NORTH);

    sliderValue.addFocusListener(
        new FocusListener() {

          private int oldValue = 0;

          public void focusGained(FocusEvent arg0) {
            oldValue = Integer.parseInt(sliderValue.getText());
          }

          public void focusLost(FocusEvent arg0) {
            int newValue = 0;
            try {
              newValue = Integer.parseInt(sliderValue.getText());
            } catch (Exception ex) {
              sliderValue.setText(Integer.toString(oldValue));
            }
            if (newValue >= 0 && newValue <= 359) {

              RotateCommand rc = new RotateCommand(board.getSelectedItems());
              ArrayList<Command> actCommands = new ArrayList<Command>();
              actCommands.add(rc);
              TBE.getInstance().addCommands(actCommands);
              rotateSlider.setValue(newValue);
            } else {
              sliderValue.setText(Integer.toString(oldValue));
            }
          }
        });

    rotateSlider.addChangeListener(
        new ChangeListener() {

          public void stateChanged(ChangeEvent arg0) {

            if (board.getSelectionCount() == 1
                && board.getSelectionCells()[0] instanceof ShapeItem) {
              sliderValue.setText(Integer.toString(rotateSlider.getValue()));
              ShapeItem s = (ShapeItem) board.getSelectionCells()[0];
              board.removeItem(new ItemComponent[] {s});
              s.setRotation(rotateSlider.getValue());
              board.addItem(s);
            }
          }
        });
    rotateSlider.addMouseListener(
        new MouseAdapter() {

          private int value;

          public void mousePressed(MouseEvent e) {
            value = rotateSlider.getValue();
          }

          public void mouseReleased(MouseEvent e) {
            if (value != rotateSlider.getValue()) {
              RotateCommand rc = new RotateCommand(board.getSelectedItems());
              ArrayList<Command> actCommands = new ArrayList<Command>();
              actCommands.add(rc);
              TBE.getInstance().addCommands(actCommands);
              rc.setRotation(value);
            }
          }
        });

    rotate.setToolTipText(workingViewLabels.getString("rotate"));

    rotate.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (board.getSelectionCount() == 1
                && board.getSelectedItems()[0] instanceof ShapeItem) {
              rotateSlider.setValue(((ShapeItem) board.getSelectedItems()[0]).getRotation());
            }
            rotatePanel.setVisible(!rotatePanel.isVisible());
            showRotate = !showRotate;
          }
        });

    rotate.setContentAreaFilled(false);
    rotate.setBorderPainted(false);
    toolbar.add(rotate);
    rotatePanel.setVisible(false);
    this.add(rotatePanel, BorderLayout.EAST);
  }