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;
 
 
 
A panel for picking SelectQuery orderings.

Author(s):
Andrei Adamchik
 
 public class SelectQueryOrderingTab extends JPanel {
 
     static final Dimension BROWSER_CELL_DIM = new Dimension(150, 100);
     static final Dimension TABLE_DIM = new Dimension(460, 60);
 
     static final String PATH_HEADER = "Path";
     static final String ASCENDING_HEADER = "Ascending";
     static final String IGNORE_CASE_HEADER = "Ignore Case";
 
     static final String REAL_PANEL = "real";
     static final String PLACEHOLDER_PANEL = "placeholder";
 
     protected ProjectController mediator;
     protected SelectQuery selectQuery;
 
     protected MultiColumnBrowser browser;
     protected JTable table;
 
     protected CardLayout cardLayout;
     protected JPanel messagePanel;
 
     public SelectQueryOrderingTab(ProjectController mediator) {
         this. = mediator;
 
         initView();
         initController();
     }
 
     protected void initView() {
 
          = new JPanel(new BorderLayout());
          = new CardLayout();
 
         JPanel mainPanel = new JPanel(new BorderLayout());
         mainPanel.add(createEditorPanel(), .);
         mainPanel.add(createSelectorPanel(), .);
 
         setLayout();
         add(mainPanel);
         add();
    }
    protected void initController() {
        // scroll to selected row whenever a selection even occurs
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    UIUtil.scrollToSelectedRow();
                }
            }
        });
    }
    protected void initFromModel() {
        Query query = .getCurrentQuery();
        if (!(query instanceof SelectQuery)) {
            processInvalidModel("Unknown query.");
            return;
        }
        if (!(((SelectQuery)query).getRoot() instanceof Entity)) {
            processInvalidModel("SelectQuery has no root set.");
            return;
        }
        this. = (SelectQueryquery;
        .setModel(createTableModel());
        // init column sizes
        .show(this);
    }
    protected void processInvalidModel(String message) {
        JLabel messageLabel = new JLabel(message.);
        .removeAll();
        .add(messageLabel.);
        .show(this);
    }
    protected JPanel createEditorPanel() {
         = new JTable();
        .setRowHeight(25);
        .setRowMargin(3);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(), .);
        return panel;
    }
    protected JPanel createSelectorPanel() {
         = new MultiColumnBrowser();
        .setDefaultRenderer();
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(createToolbar(), .);
        panel.add(new JScrollPane(
                ,
                .,
        return panel;
    }
    protected JComponent createToolbar() {
        JButton add = new JButton("Add Ordering", ModelerUtil
                .buildIcon("icon-move_up.gif"));
        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addOrdering();
            }
        });
        JButton remove = new JButton("Remove Ordering", ModelerUtil
                .buildIcon("icon-move_down.gif"));
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeOrdering();
            }
        });
        JToolBar toolbar = new JToolBar();
        toolbar.add(add);
        toolbar.add(remove);
        return toolbar;
    }
    protected TreeModel createBrowserModel(Entity entity) {
        return new EntityTreeModel(entity);
    }
    protected TableModel createTableModel() {
        return new OrderingModel();
    }
    protected String getSelectedPath() {
        Object[] path = .getSelectionPath().getPath();
        // first item in the path is Entity, so we must have
        // at least two elements to constitute a valid ordering path
        if (path != null && path.length < 2) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        // attribute or relationships
        CayenneMapEntry first = (CayenneMapEntrypath[1];
        buffer.append(first.getName());
        for (int i = 2; i < path.lengthi++) {
            CayenneMapEntry pathEntry = (CayenneMapEntrypath[i];
            buffer.append(".").append(pathEntry.getName());
        }
        return buffer.toString();
    }
    void addOrdering() {
        String orderingPath = getSelectedPath();
        if (orderingPath == null) {
            return;
        }
        // check if such ordering already exists
        Iterator it = .getOrderings().iterator();
        while (it.hasNext()) {
            Ordering ord = (Orderingit.next();
            if (orderingPath.equals(ord.getSortSpecString())) {
                return;
            }
        }
        .addOrdering(new Ordering(orderingPath.));
        int index = .getOrderings().size() - 1;
        OrderingModel model = (OrderingModel.getModel();
        model.fireTableRowsInserted(indexindex);
    }
    void removeOrdering() {
        int selection = .getSelectedRow();
        if (selection < 0) {
            return;
        }
        OrderingModel model = (OrderingModel.getModel();
        Ordering ordering = model.getOrdering(selection);
        .removeOrdering(ordering);
        model.fireTableRowsDeleted(selectionselection);
    }

    
A table model for the Ordering editing table.
    final class OrderingModel extends AbstractTableModel {
        Ordering getOrdering(int row) {
            return (Ordering.getOrderings().get(row);
        }
        public int getColumnCount() {
            return 3;
        }
        public int getRowCount() {
            return ( != null) ? .getOrderings().size() : 0;
        }
        public Object getValueAt(int rowint column) {
            Ordering ordering = getOrdering(row);
            switch (column) {
                case 0:
                    return ordering.getSortSpecString();
                case 1:
                    return ordering.isAscending() ? . : .;
                case 2:
                    return ordering.isCaseInsensitive() ? . : .;
                default:
                    throw new IndexOutOfBoundsException("Invalid column: " + column);
            }
        }
        public Class getColumnClass(int column) {
            switch (column) {
                case 0:
                    return String.class;
                case 1:
                case 2:
                    return Boolean.class;
                default:
                    throw new IndexOutOfBoundsException("Invalid column: " + column);
            }
        }
        public String getColumnName(int column) {
            switch (column) {
                case 0:
                    return ;
                case 1:
                    return ;
                case 2:
                    return ;
                default:
                    throw new IndexOutOfBoundsException("Invalid column: " + column);
            }
        }
        public boolean isCellEditable(int rowint column) {
            return column == 1 || column == 2;
        }
        public void setValueAt(Object valueint rowint column) {
            Ordering ordering = getOrdering(row);
            switch (column) {
                case 1:
                    ordering.setAscending(((Booleanvalue).booleanValue());
                    .fireQueryEvent(new QueryEvent(
                            SelectQueryOrderingTab.this,
                            ));
                    break;
                case 2:
                    ordering.setCaseInsensitive(((Booleanvalue).booleanValue());
                    .fireQueryEvent(new QueryEvent(
                            SelectQueryOrderingTab.this,
                            ));
                    break;
                default:
                    throw new IndexOutOfBoundsException("Invalid editable column: "
                            + column);
            }
        }
    }
New to GrepCode? Check out our FAQ X