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 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.dialog.db;
 import  org.apache.cayenne.access.DbGenerator;
 import  org.apache.cayenne.dba.DbAdapter;
 import  org.apache.cayenne.validation.ValidationResult;

Andrei Adamchik
 public class DBGeneratorOptions extends CayenneController {
     protected DBGeneratorOptionsView view;
     protected ObjectBinding[] optionBindings;
     protected ObjectBinding sqlBinding;
     protected DBConnectionInfo connectionInfo;
     protected DataMap dataMap;
     protected DbGenerator generator;
     protected String textForSQL;
     protected TableSelectorController tables;
     public DBGeneratorOptions(ProjectController parentString title,
             DBConnectionInfo connectionInfo, DataMap dataMap) {
         this. = dataMap;
         this. = new TableSelectorController(parent);
         this. = new DBGeneratorOptionsView(.getView());
         this. = connectionInfo;
         this. = (DBGeneratorDefaultsparent
     public Component getView() {
         return ;
         return ;
     public String getTextForSQL() {
         return ;
    protected void initController() {
        BindingBuilder builder = new BindingBuilder(
         = builder.bindToTextArea(.getSql(), "textForSQL");
         = new ObjectBinding[5];
        builder.bindToAction(.getGenerateButton(), "generateSchemaAction()");
        builder.bindToAction(.getSaveSqlButton(), "storeSQLAction()");
        builder.bindToAction(.getCancelButton(), "closeAction()");
        // refresh SQL if different tables were selected
        .getTabs().addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if (.getTabs().getSelectedIndex() == 0) {
                    // this assumes that some tables where checked/unchecked... not very
                    // efficient

Creates new internal DbGenerator instance.
    protected void prepareGenerator() {
        try {
            DbAdapter adapter = .makeAdapter(getApplication()
            this. = new DbGenerator(adapter.getExcludedTables());
        catch (Exception ex) {
            reportError("Error loading adapter"ex);

Returns SQL statements generated for selected schema generation options.
    protected void createSQL() {
        // convert them to string representation for display
        StringBuffer buf = new StringBuffer();
        Iterator it = .configuredStatements().iterator();
        String batchTerminator = .getAdapter().getBatchTerminator();
        String lineEnd = (batchTerminator != null)
                ? "\n" + batchTerminator + "\n\n"
                : "\n\n";
        while (it.hasNext()) {
         = buf.toString();
    protected void refreshView() {
        for (int i = 0; i < .i++) {
    // ===============
    //    Actions
    // ===============

Starts options dialog.
    public void startupAction() {
    public void refreshGeneratorAction() {

Updates a text area showing generated SQL.
    public void refreshSQLAction() {
        // sync generator with defaults, make SQL, then sync the view...

Performs configured schema operations via DbGenerator.
    public void generateSchemaAction() {
        // sanity check...
        if (.isEmpty(true)) {
            JOptionPane.showMessageDialog(getView(), "Nothing to generate.");
        try {
            DataSource dataSource = .makeDataSource(getApplication()
            ValidationResult failures = .getFailures();
            if (failures == null || !failures.hasFailures()) {
                JOptionPane.showMessageDialog(getView(), "Schema Generation Complete.");
            else {
                new ValidationResultBrowser(this)
                        .startupAction("Schema Generation Complete",
                                "Schema generation finished. The following problem(s) were ignored.",
        catch (Throwable th) {
            reportError("Schema Generation Error"th);

Allows user to save generated SQL in a file.
    public void storeSQLAction() {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Save SQL Script");
        File projectDir = Application.getProject().getProjectDirectory();
        if (projectDir != null) {
        if (fc.showSaveDialog(getView()) == .) {
            try {
                File file = fc.getSelectedFile();
                FileWriter fw = new FileWriter(file);
                PrintWriter pw = new PrintWriter(fw);
            catch (IOException ex) {
                reportError("Error Saving SQL"ex);
    public void closeAction() {
New to GrepCode? Check out our FAQ X