Exemple #1
0
  /**
   * Standard constructor: it needs the parent frame.
   *
   * @param parent the dialog's parent
   */
  public DialogPrint(JFrame parent) {
    super(400, 350, parent, Globals.messages.getString("Print_dlg"), true);
    addComponentListener(this);
    export = false;

    // Ensure that under MacOSX >= 10.5 Leopard, this dialog will appear
    // as a document modal sheet

    getRootPane().putClientProperty("apple.awt.documentModalSheet", Boolean.TRUE);

    GridBagLayout bgl = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    Container contentPane = getContentPane();
    contentPane.setLayout(bgl);

    constraints.insets.right = 30;

    JLabel empty = new JLabel("  ");
    constraints.weightx = 100;
    constraints.weighty = 100;
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    contentPane.add(empty, constraints); // Add "   " label

    JLabel empty1 = new JLabel("  ");
    constraints.weightx = 100;
    constraints.weighty = 100;
    constraints.gridx = 3;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    contentPane.add(empty1, constraints); // Add "   " label

    mirror_CB = new JCheckBox(Globals.messages.getString("Mirror"));
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.WEST;
    contentPane.add(mirror_CB, constraints); // Add Print Mirror cb

    fit_CB = new JCheckBox(Globals.messages.getString("FitPage"));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.WEST;
    contentPane.add(fit_CB, constraints); // Add Fit to page cb

    bw_CB = new JCheckBox(Globals.messages.getString("B_W"));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.WEST;
    contentPane.add(bw_CB, constraints); // Add BlackWhite cb

    landscape_CB = new JCheckBox(Globals.messages.getString("Landscape"));
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.WEST;
    contentPane.add(landscape_CB, constraints); // Add landscape cb

    // Put the OK and Cancel buttons and make them active.
    JButton ok = new JButton(Globals.messages.getString("Ok_btn"));
    JButton cancel = new JButton(Globals.messages.getString("Cancel_btn"));

    constraints.gridx = 0;
    constraints.gridy = 4;
    constraints.gridwidth = 4;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.EAST;

    // Put the OK and Cancel buttons and make them active.
    Box b = Box.createHorizontalBox();
    b.add(Box.createHorizontalGlue());
    ok.setPreferredSize(cancel.getPreferredSize());

    if (Globals.okCancelWinOrder) {
      b.add(ok);
      b.add(Box.createHorizontalStrut(12));
      b.add(cancel);

    } else {
      b.add(cancel);
      b.add(Box.createHorizontalStrut(12));
      b.add(ok);
    }

    ok.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            export = true;
            setVisible(false);
          }
        });
    cancel.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            setVisible(false);
          }
        });
    // Here is an action in which the dialog is closed

    AbstractAction cancelAction =
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            setVisible(false);
          }
        };
    contentPane.add(b, constraints); // Add OK/cancel dialog

    DialogUtil.addCancelEscape(this, cancelAction);
    pack();
    DialogUtil.center(this);
    getRootPane().setDefaultButton(ok);
  }
    public void actionPerformed(ActionEvent e) {
      int tempW = mapWidth;
      int tempH = mapHeight;
      int targetW;
      int targetH;
      try {
        targetW = Integer.parseInt(widthField.getText());
        targetH = Integer.parseInt(heightField.getText());

        if (targetH <= 0 || targetW <= 0) {
          JOptionPane.showMessageDialog(null, "Both x and y must be above 0.");
        } else {

          // shrink width if necessary
          while (targetW < mapWidth) {
            for (int i = mapHeight * mapWidth - 1; i >= 0; i -= mapWidth) {
              map.remove(i);
            }
            mapWidth--;
            mapScroll.revalidate();
            map.repaint();

            backEnd.removeColumn(backEnd.getWidth() - 1);
          }

          // shrink height if necessary
          while (targetH < mapHeight) {
            for (int i = 1; i <= mapWidth; i++) {
              map.remove(mapHeight * mapWidth - i);
            }
            mapHeight--;
            // map.setLayout(new GridLayout(mapHeight,mapWidth));
            map.repaint();
            backEnd.removeRow(backEnd.getHeight() - 1);
          }

          // Grow if necessary
          if (targetW > mapWidth || targetH > mapHeight) {
            // add new rows and columns, then rebuild and re-add display
            int[] mov = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
            while (targetW > mapWidth) {
              // add new column to backEnd
              List<Tile> tList = new ArrayList<Tile>();
              for (int i = 0; i < mapHeight; i++) {
                Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc);
                t.setPreferredSize(
                    new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE));
                t.addMouseListener(new MapButtonListener());
                tList.add(t);
              }
              backEnd.addColumn(tList);
              mapWidth++;
            }
            while (targetH > mapHeight) {
              // add new row to backEnd
              List<Tile> tList = new ArrayList<Tile>();
              for (int i = 0; i < mapWidth; i++) {
                Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc);
                t.setPreferredSize(
                    new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE));
                t.addMouseListener(new MapButtonListener());
                tList.add(t);
              }
              backEnd.addRow(tList);
              mapHeight++;
            }

            GridBagConstraints gbc = new GridBagConstraints();

            for (int i = 0; i < mapHeight; i++) {
              gbc.gridy = i;
              for (int j = 0; j < mapWidth; j++) {
                gbc.gridx = j;
                map.add(backEnd.getTile(j, i), gbc);
              }
            }
            map.revalidate();
            map.repaint();
            parentPanel.revalidate();
            parentPanel.repaint();
            ((MapBuilder) SwingUtilities.getWindowAncestor(parentPanel)).pack();
          }
        }
      } catch (NumberFormatException f) {
        JOptionPane.showMessageDialog(null, "Both x and y must be valid integers.");
      }
    }
  /**
   * Builds a blank display. Eventually I'll have to add loading from a map, though right now it
   * builds the map itself. I should separate that out.
   */
  public JComponent buildDisplay() {
    JPanel holder = new JPanel(new GridBagLayout());
    map = holder;

    // holder.setLayout(new GridLayout(mapHeight,mapWidth));
    // holder.setPreferredSize(new Dimension(mapWidth * TILE_SIZE * DISPLAY_SCALE,
    // 									mapHeight * TILE_SIZE * DISPLAY_SCALE));
    // holder.setMaximumSize(new Dimension(mapWidth * TILE_SIZE * DISPLAY_SCALE,
    //  									mapHeight * TILE_SIZE * DISPLAY_SCALE));
    // backEnd = emptyMap(mapWidth, mapHeight);

    GridBagConstraints gbc = new GridBagConstraints();

    // for(int i = 0; i < mapHeight; i++){
    // 	List<Tile> tList = new ArrayList<Tile>();
    // 	gbc.gridy = i;
    // 	for(int j = 0; j < mapWidth; j++){
    // 		gbc.gridx = j;
    // 		Tile t = new Tile(currTileImg, "Test", 0, 0, mov,"none",true, "0_0");
    // 		tList.add(t);
    // 		//t.setMargin(new Insets(0,0,0,0));
    // 		t.setMaximumSize(new Dimension(TILE_SIZE*DISPLAY_SCALE,TILE_SIZE*DISPLAY_SCALE));
    // 		t.setPreferredSize(new Dimension(TILE_SIZE*DISPLAY_SCALE, TILE_SIZE*DISPLAY_SCALE));
    // 		//tile.setPreferredSize(new Dimension(TILE_SIZE*DISPLAY_SCALE, TILE_SIZE*DISPLAY_SCALE));
    // 		t.addMouseListener(new MapButtonListener());
    // 		holder.add(t,gbc);

    // 	}
    // 	backEnd.addRow(tList);
    // }

    for (int i = 0; i < mapHeight; i++) {
      gbc.gridy = i;
      for (int j = 0; j < mapWidth; j++) {
        gbc.gridx = j;
        holder.add(backEnd.getTile(j, i), gbc);
      }
    }
    // gbc.gridy = 0;
    // gbc.gridx = mapWidth;
    // gbc.gridheight = GridBagConstraints.REMAINDER;
    // gbc.weightx = 1;
    // gbc.weighty = 1;
    // gbc.fill = GridBagConstraints.BOTH;
    // holder.add(new JPanel(), gbc);
    // gbc.gridx = 0;
    // gbc.gridy = mapHeight;
    // gbc.gridheight = 1;
    // gbc.gridwidth = GridBagConstraints.REMAINDER;
    // holder.add(new JPanel(), gbc);
    // System.out.println(backEnd);

    // Container panel to prevent stretching
    JPanel outer = new JPanel();
    outer.add(holder);

    // BoxLayout ensures that maxSize is honored
    // JPanel displaySizeRegulator = new JPanel();
    // displaySizeRegulator.setLayout(new BoxLayout(displaySizeRegulator, BoxLayout.X_AXIS));

    JScrollPane displayScroll =
        new JScrollPane(
            outer,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    mapScroll = displayScroll;

    // displaySizeRegulator.add(displayScroll);
    // displayRefresher = displaySizeRegulator;
    return displayScroll;
  }
Exemple #4
0
  public void run() {

    // Collect some user data.
    dlg = new JDialog(console.frame, "Example 1 Setup", true);
    dlg.getContentPane().setLayout(new BorderLayout());

    // Properties area
    propPanel = new JPanel();
    dlg.getContentPane().add(propPanel, BorderLayout.CENTER);

    GridBagLayout gbl = new GridBagLayout();
    propPanel.setLayout(gbl);

    // List of voice resources
    DefaultListModel dlm = new DefaultListModel();
    for (int x = 1; ; x++) {
      try {
        int c = x % 4 == 0 ? 4 : x % 4;
        int b = c == 4 ? x / 4 : x / 4 + 1;
        String devName = "dxxxB" + b + "C" + c;
        int dev = dx.open(devName, 0);
        try {
          // If any of the voice resources _are not_ connected to
          // analog loop-start lines, then they will be suppressed
          // here because sethook() will not be supported.
          dx.sethook(dev, dx.DX_ONHOOK, dx.EV_SYNC);
          dlm.addElement(devName);
        } catch (Exception ignore) {
        }
        dx.close(dev);
      } catch (Exception ignore) {
        break;
      }
    }
    analogDxList = new JList(dlm);
    {
      GridBagConstraints gbc;
      // Choose a voice resource:
      gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.fill = GridBagConstraints.BOTH;
      JTextField t = new JTextField("Analog Voice Resource");
      t.setEditable(false);
      gbl.setConstraints(t, gbc);
      propPanel.add(t);
      gbc = new GridBagConstraints();
      gbc.gridx = 1;
      gbc.gridy = 0;
      gbc.fill = GridBagConstraints.BOTH;
      JScrollPane s = new JScrollPane(analogDxList);
      gbl.setConstraints(s, gbc);
      propPanel.add(s);
    }

    // Dialog buttons at the bottom.
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    dlg.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    // "Run"
    {
      JButton b = new JButton("Run");
      b.addActionListener(
          new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
              final Object[] dx = analogDxList.getSelectedValues();
              if (dx.length != 1) {
                // "Please select one, and only one, voice resource."
                JOptionPane.showMessageDialog(
                    dlg,
                    "Please select one, and only one, resource.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
              }
              Thread t =
                  new Thread() {
                    public void run() {
                      runExample((String) dx[0]);
                    }
                  };
              t.start();
              dlg.dispose();
            }
          });
      buttonPanel.add(b);
    }
    // "Cancel"
    {
      JButton b = new JButton("Cancel");
      b.addActionListener(
          new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
              dlg.dispose();
            }
          });
      buttonPanel.add(b);
    }

    // Pack and Show
    dlg.pack();
    dlg.setLocationRelativeTo(console.frame);
    dlg.setVisible(true);
  }