private ActionToolbar createToolbar() {
    DefaultActionGroup group = new DefaultActionGroup();

    BackAction back = new BackAction();
    back.registerCustomShortcutSet(
        new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), this);
    group.add(back);

    ForwardAction forward = new ForwardAction();
    forward.registerCustomShortcutSet(
        new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0)), this);
    group.add(forward);

    EditSourceActionBase edit = new EditSourceAction();
    edit.registerCustomShortcutSet(
        new CompositeShortcutSet(CommonShortcuts.getEditSource(), CommonShortcuts.ENTER), this);
    group.add(edit);

    edit = new ShowSourceAction();
    edit.registerCustomShortcutSet(
        new CompositeShortcutSet(CommonShortcuts.getViewSource(), CommonShortcuts.CTRL_ENTER),
        this);
    group.add(edit);

    return ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true);
  }
Exemple #2
0
  public ParameterTablePanel(
      Project project, VariableData[] variableData, final PsiElement... scopeElements) {
    super(new BorderLayout());
    myProject = project;
    myVariableData = variableData;

    myTableModel = new MyTableModel();
    myTable = new JBTable(myTableModel);
    DefaultCellEditor defaultEditor = (DefaultCellEditor) myTable.getDefaultEditor(Object.class);
    defaultEditor.setClickCountToStart(1);

    myTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    myTable.setCellSelectionEnabled(true);
    TableColumn checkboxColumn = myTable.getColumnModel().getColumn(MyTableModel.CHECKMARK_COLUMN);
    TableUtil.setupCheckboxColumn(checkboxColumn);
    checkboxColumn.setCellRenderer(new CheckBoxTableCellRenderer());
    myTable
        .getColumnModel()
        .getColumn(MyTableModel.PARAMETER_NAME_COLUMN)
        .setCellRenderer(
            new DefaultTableCellRenderer() {
              public Component getTableCellRendererComponent(
                  JTable table,
                  Object value,
                  boolean isSelected,
                  boolean hasFocus,
                  int row,
                  int column) {
                super.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
                VariableData data = getVariableData()[row];
                setText(data.name);
                return this;
              }
            });

    myParameterTypeSelectors = new TypeSelector[getVariableData().length];
    for (int i = 0; i < myParameterTypeSelectors.length; i++) {
      final PsiVariable variable = getVariableData()[i].variable;
      final PsiExpression[] occurrences = findVariableOccurrences(scopeElements, variable);
      final TypeSelectorManager manager =
          new TypeSelectorManagerImpl(
              myProject, getVariableData()[i].type, occurrences, areTypesDirected()) {
            @Override
            protected boolean isUsedAfter() {
              return ParameterTablePanel.this.isUsedAfter(variable);
            }
          };
      myParameterTypeSelectors[i] = manager.getTypeSelector();
      getVariableData()[i].type = myParameterTypeSelectors[i].getSelectedType(); // reverse order
    }

    myTypeRendererCombo = new JComboBox(getVariableData());
    myTypeRendererCombo.setOpaque(true);
    myTypeRendererCombo.setBorder(null);
    myTypeRendererCombo.setRenderer(
        new ListCellRendererWrapper<VariableData>() {
          @Override
          public void customize(
              JList list, VariableData value, int index, boolean selected, boolean hasFocus) {
            if (value != null) {
              setText(value.type.getPresentableText());
            }
          }
        });

    final TableColumn typeColumn =
        myTable.getColumnModel().getColumn(MyTableModel.PARAMETER_TYPE_COLUMN);
    typeColumn.setCellEditor(
        new AbstractTableCellEditor() {
          TypeSelector myCurrentSelector;
          final JBComboBoxTableCellEditorComponent myEditorComponent =
              new JBComboBoxTableCellEditorComponent();

          @Nullable
          public Object getCellEditorValue() {
            return myEditorComponent.getEditorValue();
          }

          public Component getTableCellEditorComponent(
              final JTable table,
              final Object value,
              final boolean isSelected,
              final int row,
              final int column) {
            myEditorComponent.setCell(table, row, column);
            myEditorComponent.setOptions(myParameterTypeSelectors[row].getTypes());
            myEditorComponent.setDefaultValue(getVariableData()[row].type);
            myEditorComponent.setToString(
                new Function<Object, String>() {
                  @Override
                  public String fun(Object o) {
                    return ((PsiType) o).getPresentableText();
                  }
                });

            myCurrentSelector = myParameterTypeSelectors[row];
            return myEditorComponent;
          }
        });

    myTable
        .getColumnModel()
        .getColumn(MyTableModel.PARAMETER_TYPE_COLUMN)
        .setCellRenderer(
            new DefaultTableCellRenderer() {
              private JBComboBoxLabel myLabel = new JBComboBoxLabel();

              public Component getTableCellRendererComponent(
                  JTable table,
                  Object value,
                  boolean isSelected,
                  boolean hasFocus,
                  int row,
                  int column) {
                myLabel.setText(String.valueOf(value));
                myLabel.setBackground(
                    isSelected ? table.getSelectionBackground() : table.getBackground());
                myLabel.setForeground(
                    isSelected ? table.getSelectionForeground() : table.getForeground());
                if (isSelected) {
                  myLabel.setSelectionIcon();
                } else {
                  myLabel.setRegularIcon();
                }
                return myLabel;
              }
            });

    myTable.setPreferredScrollableViewportSize(new Dimension(250, myTable.getRowHeight() * 5));
    myTable.setShowGrid(false);
    myTable.setIntercellSpacing(new Dimension(0, 0));
    @NonNls final InputMap inputMap = myTable.getInputMap();
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "enable_disable");
    @NonNls final ActionMap actionMap = myTable.getActionMap();
    actionMap.put(
        "enable_disable",
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            if (myTable.isEditing()) return;
            int[] rows = myTable.getSelectedRows();
            if (rows.length > 0) {
              boolean valueToBeSet = false;
              for (int row : rows) {
                if (!getVariableData()[row].passAsParameter) {
                  valueToBeSet = true;
                  break;
                }
              }
              for (int row : rows) {
                getVariableData()[row].passAsParameter = valueToBeSet;
              }
              myTableModel.fireTableRowsUpdated(rows[0], rows[rows.length - 1]);
              TableUtil.selectRows(myTable, rows);
            }
          }
        });
    //// F2 should edit the name
    // inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "edit_parameter_name");
    // actionMap.put("edit_parameter_name", new AbstractAction() {
    //  public void actionPerformed(ActionEvent e) {
    //    if (!myTable.isEditing()) {
    //      int row = myTable.getSelectedRow();
    //      if (row >= 0 && row < myTableModel.getRowCount()) {
    //        TableUtil.editCellAt(myTable, row, MyTableModel.PARAMETER_NAME_COLUMN);
    //      }
    //    }
    //  }
    // });

    //// make ENTER work when the table has focus
    // inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "invokeImpl");
    // actionMap.put("invokeImpl", new AbstractAction() {
    //  public void actionPerformed(ActionEvent e) {
    //    TableCellEditor editor = myTable.getCellEditor();
    //    if (editor != null) {
    //      editor.stopCellEditing();
    //    }
    //    else {
    //      doEnterAction();
    //    }
    //  }
    // });

    // make ESCAPE work when the table has focus
    actionMap.put(
        "doCancel",
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            TableCellEditor editor = myTable.getCellEditor();
            if (editor != null) {
              editor.stopCellEditing();
            } else {
              doCancelAction();
            }
          }
        });

    JPanel listPanel =
        ToolbarDecorator.createDecorator(myTable)
            .disableAddAction()
            .disableRemoveAction()
            .createPanel();
    add(listPanel, BorderLayout.CENTER);

    if (getVariableData().length > 1) {
      myTable.getSelectionModel().setSelectionInterval(0, 0);
    }
  }