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.awt.Color;
 import java.util.List;
 import java.util.Map;
 
 
 
 import  com.jgoodies.forms.builder.PanelBuilder;
 import  com.jgoodies.forms.layout.CellConstraints;
 import  com.jgoodies.forms.layout.FormLayout;

A panel for configuring SQL scripts of a SQL template.

Author(s):
Andrei Adamchik
 
 public class SQLTemplateScriptsTab extends JPanel {
 
     private static final String DEFAULT_LABEL = "Default";
 
     protected ProjectController mediator;
 
     protected JList scripts;
     protected TextAdapter script;
 
     public SQLTemplateScriptsTab(ProjectController mediator) {
         this. = mediator;
 
         initView();
     }
 
     protected void initView() {
         // create widgets, etc.
 
          = new ListSelectionListener() {
 
             public void valueChanged(ListSelectionEvent e) {
                 if (!e.getValueIsAdjusting()) {
                     displayScript();
                 }
             }
         };
 
          = new JList();
         .setCellRenderer(new DbAdapterListRenderer(DbAdapterInfo
                 .getStandardAdapterLabels()));
 
         List keys = new ArrayList(DbAdapterInfo.getStandardAdapters().length + 1);
         keys.addAll(Arrays.asList(DbAdapterInfo.getStandardAdapters()));
         Collections.sort(keys);
         keys.add(0, );
         .setModel(new DefaultComboBoxModel(keys.toArray()));
 
         = new TextAdapter(new JTextArea(15, 30)) {
            protected void updateModel(String text) {
                setSQL(text);
            }
        };
        // assemble
        CellConstraints cc = new CellConstraints();
        PanelBuilder builder = new PanelBuilder(new FormLayout(
                "fill:100dlu, 3dlu, fill:100dlu:grow",
                "3dlu, fill:p:grow"));
        // orderings table must grow as the panel is resized
        builder.add(new JScrollPane(
                ,
                .,
                .), cc.xy(1, 2));
        builder.add(new JScrollPane(.getComponent()), cc.xy(3, 2));
        setLayout(new BorderLayout());
        add(builder.getPanel(), .);
    }
    void initFromModel() {
        Query query = .getCurrentQuery();
        if (!(query instanceof SQLTemplate)) {
            setVisible(false);
            return;
        }
        // select default script.. display it bypassing the listener...
        .setSelectedIndex(0);
        displayScript();
        .getComponent().setEnabled(true);
        setVisible(true);
    }

    
Returns SQLTemplate text for current selection.
    String getSQLTemplate(String key) {
        if (key == null) {
            return null;
        }
        SQLTemplate query = getQuery();
        if (query == null) {
            return null;
        }
        return (key.equals()) ? query.getDefaultTemplate() : query
                .getCustomTemplate(key);
    }
    SQLTemplate getQuery() {
        Query query = .getCurrentQuery();
        return (query instanceof SQLTemplate) ? (SQLTemplatequery : null;
    }

    
Shows selected script in the editor.
    void displayScript() {
        SQLTemplate query = getQuery();
        if (query == null) {
            disableEditor();
            return;
        }
        String key = (String.getSelectedValue();
        if (key == null) {
            disableEditor();
            return;
        }
        enableEditor();
        String text = (key.equals()) ? query.getDefaultTemplate() : query
                .getCustomTemplate(key);
        .setText(text);
    }
    void disableEditor() {
        .setText(null);
        .getComponent().setEnabled(false);
        .getComponent().setEditable(false);
    }
    void enableEditor() {
        .getComponent().setEnabled(true);
        .getComponent().setEditable(true);
    }
    void setSQL(DocumentEvent e) {
        Document doc = e.getDocument();
        try {
            setSQL(doc.getText(0, doc.getLength()));
        }
        catch (BadLocationException e1) {
            e1.printStackTrace();
        }
    }

    
Sets the value of SQL template for the currently selected script.
    void setSQL(String text) {
        SQLTemplate query = getQuery();
        if (query == null) {
            return;
        }
        String key = (String.getSelectedValue();
        if (key == null) {
            return;
        }
        if (text != null) {
            text = text.trim();
            if (text.length() == 0) {
                text = null;
            }
        }
        // Compare the value before modifying the query - text area
        // will call "verify" even if no changes have occured....
        if (key.equals()) {
            if (!Util.nullSafeEquals(textquery.getDefaultTemplate())) {
                query.setDefaultTemplate(text);
                .fireQueryEvent(new QueryEvent(thisquery));
            }
        }
        else {
            if (!Util.nullSafeEquals(textquery.getTemplate(key))) {
                query.setTemplate(keytext);
                .fireQueryEvent(new QueryEvent(thisquery));
            }
        }
    }
    final class DbAdapterListRenderer extends DefaultListCellRenderer {
        Map adapterLabels;
        DbAdapterListRenderer(Map adapterLabels) {
            this. = (adapterLabels != null)
                    ? adapterLabels
                    : .;
        }
        public Component getListCellRendererComponent(
                JList list,
                Object object,
                int index,
                boolean selected,
                boolean hasFocus) {
            if (object instanceof Class) {
                object = ((Classobject).getName();
            }
            Object label = .get(object);
            if (label == null) {
                label = object;
            }
            Component c = super.getListCellRendererComponent(
                    list,
                    label,
                    index,
                    selected,
                    hasFocus);
            // grey out keys that have no SQL
            setForeground(selected || getSQLTemplate(object.toString()) != null
                    ? .
                    : .);
            return c;
        }
    }
New to GrepCode? Check out our FAQ X