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  org.apache.cayenne.map.event.QueryEvent;
 import  org.apache.cayenne.query.Query;
 import  org.apache.cayenne.query.SQLTemplate;
 import  org.apache.cayenne.util.Util;
 
 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.
 
 public class SQLTemplateScriptsTab extends JPanel implements DocumentListener {
 
     private static final String DEFAULT_LABEL = "Default";
    
    
JEdit marker for SQL Template
 
     static final TokenMarker SQL_TEMPLATE_MARKER;
     static {
         KeywordMap map = PLSQLTokenMarker.getKeywordMap();
         
         //adding more keywords
         map.add("FIRST".);
         map.add("LIMIT".);
         map.add("OFFSET".);
         map.add("TOP".);
         
         //adding velocity template highlighing
         map.add("#bind".);
         map.add("#bindEqual".);
         map.add("#bindNotEqual".);
         map.add("#bindObjectEqual".);
         map.add("#bindObjectNotEqual".);
         map.add("#chain".);
         map.add("#chunk".);
         map.add("#end".);
         map.add("#result".);
         
          = new SQLTokenMarker(map);
     }
 
     protected ProjectController mediator;
 
     protected JList scripts;
    
    
JEdit text component for highlighing SQL syntax (see CAY-892)
    protected JEditTextArea scriptArea;
    
    
Indication that no update should be fired
    private boolean updateDisabled;
    
    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()));
        
         = CayenneWidgetFactory.createJEditTextArea();
        
        
        // 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(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();
        
        .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) ? (SQLTemplate) query : 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);
         = true;
        .setText(text);
         = false;
    }
    void disableEditor() {
        .setText(null);
        .setEnabled(false);
        .setEditable(false);
    }
    void enableEditor() {
        .setEnabled(true);
        .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));
            }
        }
    }
    
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
    
    public void changedUpdate(DocumentEvent e) {
        if (!) {
            setSQL(e);
        }
    }
    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;
        }
    }