Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 
 package org.apache.cayenne.modeler.editor;
 
 import java.util.List;
 
 
 import  org.apache.cayenne.dba.TypesMapping;
 import  org.apache.cayenne.map.Procedure;
 import  org.apache.cayenne.map.ProcedureParameter;
 import  org.apache.cayenne.map.event.MapEvent;
 import  org.apache.cayenne.map.event.ProcedureEvent;
 import  org.apache.cayenne.map.event.ProcedureParameterEvent;
 import  org.apache.cayenne.map.event.ProcedureParameterListener;
 
 public class ProcedureParameterTab
     extends JPanel
     implements
         ProcedureParameterListener,
         ProcedureDisplayListener,
         ExistingSelectionProcessor,
         ActionListener {
     
     protected ProjectController eventController;
 
     protected CayenneTable table;
     protected JButton removeParameterButton;
     protected JButton moveUp;
     protected JButton moveDown;
    
    
By now popup menu items are made similiar to toolbar button. (i.e. all functionality is here) This should be probably refactored as Action.
 
     protected JMenuItem removeParameterMenu;
     protected JMenuItem moveUpMenu;
     protected JMenuItem moveDownMenu;
 
     public ProcedureParameterTab(ProjectController eventController) {
         this. = eventController;
 
         init();
 
        eventController.addProcedureDisplayListener(this);
        eventController.addProcedureParameterListener(this);
            public void valueChanged(ListSelectionEvent e) {
                processExistingSelection(e);
            }
        });
        
        .addActionListener(this);
        .addActionListener(this);
        
        .addActionListener(this);
        .addActionListener(this);
    }
    protected void init() {
        setLayout(new BorderLayout());
        JToolBar toolBar = new JToolBar();
        Application app = Application.getInstance();
        toolBar.add(app.getAction(CreateProcedureParameterAction.getActionName()).buildButton());
         = app.getAction(RemoveProcedureParameterAction.getActionName()).buildButton();
        toolBar.add();
        toolBar.addSeparator();
        
        Icon up = ModelerUtil.buildIcon("icon-move_up.gif");
        Icon down = ModelerUtil.buildIcon("icon-move_down.gif");
         = new JButton();
        .setIcon(up);
        .setToolTipText("Move Parameter Up");
        toolBar.add();
        
         = new JButton();
        .setIcon(down);
        .setToolTipText("Move Parameter Down");
        toolBar.add();
        
        toolBar.addSeparator();
        toolBar.add(app.getAction(CutProcedureParameterAction.getActionName()).buildButton());
        toolBar.add(app.getAction(CopyProcedureParameterAction.getActionName()).buildButton());
        toolBar.add(app.getAction(PasteAction.getActionName()).buildButton());
        
        add(toolBar.);
        // Create table with two columns and no rows.
         = new CayenneTable();
        
        
Create and install a popup
        JPopupMenu popup = new JPopupMenu();
        
         = app.getAction(RemoveProcedureParameterAction.getActionName()).buildMenu(); 
        
        popup.add();
        popup.addSeparator();
        
         = new JMenuItem("Move Parameter Up"up);
         = new JMenuItem("Move Parameter Down"down);
        
        popup.add();
        popup.add();
        
        popup.addSeparator();
        popup.add(app.getAction(CutProcedureParameterAction.getActionName()).buildMenu());
        popup.add(app.getAction(CopyProcedureParameterAction.getActionName()).buildMenu());
        popup.add(app.getAction(PasteAction.getActionName()).buildMenu());
        
        TablePopupHandler.install(popup);
        
        add(PanelFactory.createTablePanel(null), .);
        
                CutProcedureParameterAction.getActionName(), 
                CopyProcedureParameterAction
                
                .getActionName());
    }
    
    public void processExistingSelection(EventObject e) {
        if (e instanceof ChangeEvent){
            .clearSelection();
        }
        ProcedureParameter[] parameters = new ProcedureParameter[0];
        boolean enableUp = false;
        boolean enableDown = false;
        boolean enableRemoveButton = false;
        int selectedRow = .getSelectedRow();
        if (selectedRow >= 0) {
            enableRemoveButton = true;
            ProcedureParameterTableModel model =
                (ProcedureParameterTableModel.getModel();
            
            int[] sel = .getSelectedRows();
            parameters = new ProcedureParameter[sel.length];
            
            for (int i = 0; i < sel.lengthi++) {
                parameters[i] = model.getParameter(sel[i]);
            }
            if (sel.length == 1) {
                // scroll table
                UIUtil.scrollToSelectedRow();
                int rowCount = .getRowCount();
                if (rowCount > 1) {
                    if (selectedRow >0) {
                        enableUp = true;
                    }
                    if (selectedRow < (rowCount - 1)) {
                        enableDown = true;
                    }
                }
            }
        }
        .setEnabled(enableRemoveButton);
        .setEnabled(enableUp);
        .setEnabled(enableDown);
        
        syncButtons();
        ProcedureParameterDisplayEvent ppde =
            new ProcedureParameterDisplayEvent(
                this,
                parameters,
                .getCurrentProcedure(),
                .getCurrentDataMap(),
                .getCurrentDataDomain());
    }

    
Invoked when currently selected Procedure object is changed.
        Procedure procedure = e.getProcedure();
        if (procedure != null && e.isProcedureChanged()) {
            rebuildTable(procedure);
        }
    }

    
Selects a specified parameters.
    public void selectParameters(ProcedureParameter[] parameters) {
        ModelerUtil.updateActions(parameters.length,  
                RemoveProcedureParameterAction.getActionName(),
                CutProcedureParameterAction.getActionName(),
                CopyProcedureParameterAction.getActionName());
        ProcedureParameterTableModel model =
            (ProcedureParameterTableModel.getModel();
        
        List listAttrs = model.getObjectList();
        int[] newSel = new int[parameters.length];
        
        for (int i = 0; i < parameters.length; i++) {
            newSel[i] = listAttrs.indexOf(parameters[i]);
        }
        
        .select(newSel);
    }
    protected void rebuildTable(Procedure procedure) {
        ProcedureParameterTableModel model =
            new ProcedureParameterTableModel(procedurethis);
        .setModel(model);
        .setRowHeight(25);
        .setRowMargin(3);
        // number column tweaking
        TableColumn numberColumn =
            .getColumnModel().getColumn(
                .);
        numberColumn.setPreferredWidth(35);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        numberColumn.setCellRenderer(renderer);
        // name column tweaking
        TableColumn nameColumn =
        nameColumn.setMinWidth(150);
        // types column tweaking
        TableColumn typesColumn =
        typesColumn.setMinWidth(90);
        JComboBox typesEditor =
            CayenneWidgetFactory.createComboBox(TypesMapping.getDatabaseTypes(), true);
        AutoCompletion.enable(typesEditor);
        typesColumn.setCellEditor(CayenneWidgetFactory.createCellEditor(typesEditor));
        // direction column tweaking
        TableColumn directionColumn =
            .getColumnModel().getColumn(
                .);
        directionColumn.setMinWidth(90);
        JComboBox directionEditor =
            CayenneWidgetFactory.createComboBox(
                false);
        directionEditor.setEditable(false);
        directionColumn.setCellEditor(new CayenneCellEditor(directionEditor));
        .setEnabled(false);
        .setEnabled(false);
    }
    public void procedureParameterAdded(ProcedureParameterEvent e) {
        rebuildTable(e.getParameter().getProcedure());
        .select(e.getParameter());
    }
    public void procedureParameterChanged(ProcedureParameterEvent e) {
        .select(e.getParameter());
    }
    public void procedureParameterRemoved(ProcedureParameterEvent e) {
        ProcedureParameterTableModel model =
            (ProcedureParameterTableModel.getModel();
        int ind = model.getObjectList().indexOf(e.getParameter());
        model.removeRow(e.getParameter());
        .select(ind);
    }
    public void actionPerformed(ActionEvent e) {
        ProcedureParameterTableModel model =
            (ProcedureParameterTableModel.getModel();
        ProcedureParameter parameter = model.getParameter(.getSelectedRow());
        
        int index = -1;
        if (e.getSource() ==  || e.getSource() == ) {
            index = model.moveRowUp(parameter);
        }
        else if (e.getSource() ==  || e.getSource() == ) {
            index = model.moveRowDown(parameter);
        }
        if (index >= 0) {
            .select(index);
            
            // note that 'setCallParameters' is donw by copy internally
            parameter.getProcedure().setCallParameters(model.getObjectList());
            .fireProcedureEvent(
                new ProcedureEvent(thisparameter.getProcedure(), MapEvent.CHANGE));
        }
    }
    
    
Synchronizes state of toolbar and popup menu buttons
    private void syncButtons() {
        .setEnabled(.isEnabled());   
    }
New to GrepCode? Check out our FAQ X