Subversion Repositories XServices

Rev

Rev 30 | Rev 52 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 *   Mylyn Connector for Serena Business Mashups
 *       Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   Licensed 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.
 * 
 *   Serena, TeamTrack and Serena Business Mashup are 
 *       registered trademarks of SERENA Software Inc.
 */
package net.brutex.mylyn.sbmconnector.ui;

import java.util.List;

import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
import net.brutex.mylyn.sbmconnector.core.SBMClient;
import net.brutex.mylyn.sbmconnector.core.SBMRepositoryConnector;
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
import net.brutex.mylyn.sbmconnector.core.model.SBMSystemFields;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class SBMRepositorySettingsPage extends AbstractRepositorySettingsPage {

        private Combo table;
        private Table list;

        private String solutionTable = "";
        private String solutionTableList = "";
        private String tableFieldList = "";
        private String tableFullFieldList = "";

        public static final String STRING_SOLUTIONTABLE = "solutionTable";
        public static final String STRING_SOLUTIONTABLELIST = "solutionTableList";
        public static final String STRING_TABLEFIELDLIST = "tableFieldList";
        public static final String STRING_TABLEFULLFIELDLIST = "tableFullFieldList";

        public SBMRepositorySettingsPage(String title, String description,
                        TaskRepository taskRepository) {
                super(title, description, taskRepository);
                setNeedsAdvanced(false);
                setNeedsValidation(true);
                setNeedsEncoding(false);
                // initialize table and table list if not null
                if (taskRepository.hasProperty(STRING_SOLUTIONTABLE)) {
                        this.solutionTable = taskRepository
                                        .getProperty(STRING_SOLUTIONTABLE);
                }
                if (taskRepository.hasProperty(STRING_SOLUTIONTABLELIST)) {
                        this.solutionTableList = taskRepository
                                        .getProperty(STRING_SOLUTIONTABLELIST);
                }
                if (taskRepository.hasProperty(STRING_TABLEFIELDLIST)) {
                        this.tableFieldList = taskRepository
                                        .getProperty(STRING_TABLEFIELDLIST);
                }
                if (taskRepository.hasProperty(STRING_TABLEFULLFIELDLIST)) {
                        this.tableFullFieldList = taskRepository
                                        .getProperty(STRING_TABLEFULLFIELDLIST);
                }
        }

        @Override
        public void applyTo(TaskRepository repository) {
                super.applyTo(repository);
                if (!solutionTable.isEmpty()) {
                        repository.setProperty(STRING_SOLUTIONTABLE, solutionTable);
                        repository.setProperty(STRING_SOLUTIONTABLELIST, solutionTableList);
                        String fieldlist = ",";
                        String fullfieldlist = ",";
                        for (TableItem t : list.getItems()) {
                                if (t.getChecked())
                                        fieldlist = fieldlist + t.getText(2) + ",";
                                fullfieldlist = fullfieldlist + t.getText(2) + ",";
                        }
                        repository.setProperty(STRING_TABLEFIELDLIST, fieldlist);
                        repository.setProperty(STRING_TABLEFULLFIELDLIST, fullfieldlist);
                }
        }

        @Override
        public boolean isPageComplete() {
                if (table != null && table.getText().length() > 4) {
                        return super.isPageComplete();
                }
                return false;
        }

        @Override
        protected void createContributionControls(Composite parentControl) {
                ExpandableComposite expander = createSection(parentControl,
                                "Mashup application");
                FormToolkit toolkit = new FormToolkit(parentControl.getDisplay());
                Composite control = toolkit.createComposite(expander, SWT.NONE);
                GridLayout gridLayout = new GridLayout();
                gridLayout.numColumns = 2;
                gridLayout.verticalSpacing = 5;
                gridLayout.marginWidth = 0;
                control.setLayout(gridLayout);
                control.setBackground(parentControl.getBackground());

                Label label = new Label(control, SWT.NONE);
                label.setText("Table:");

                table = new Combo(control, SWT.SINGLE | SWT.BORDER);
                Label label_list = new Label(control, SWT.NONE | SWT.TOP);
                label_list.setText("Fields:");
                list = new Table(control, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL
                                | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.CHECK);
                list.setLinesVisible(true);
                list.setHeaderVisible(true);
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
                data.heightHint = 200;
                list.setLayoutData(data);
                String[] titles = { " ", "Display Name", "DBName", "Type" };
                for (int i = 0; i < titles.length; i++) {
                        TableColumn column = new TableColumn(list, SWT.NONE);
                        column.setText(titles[i]);
                }
                list.addListener(SWT.Selection, new Listener() {
                        @Override
                        public void handleEvent(Event event) {
                                //avoid that a system field may be unchecked
                                try {
                                        SBMSystemFields
                                                        .valueOf(((TableItem) event.item).getText(2));
                                        ((TableItem) event.item).setChecked(true);
                                } catch (IllegalArgumentException e) {
                                        // do nothing
                                }
                        }
                });
                table.addListener(SWT.Selection, new Listener() {
                        @Override
                        public void handleEvent(Event arg0) {
                                // when the table has been changed
                                // try to re-read the field list
                                tableFieldList = "";
                                tableFullFieldList = "";
                                try {
                                        SBMField[] fields = SBMRepositoryConnector.getClient(
                                                createTaskRepository()).getFields(table.getText())
                                                .toArray(new SBMField[0]);
                                fillList(list, fields);
                                } catch (Exception e) { }
                                if (getWizard() != null) {
                                        solutionTable = table.getText();
                                        getWizard().getContainer().updateButtons();
                                }
                        }

                });
                regenerateTables();
                expander.setClient(control);
        }

        @Override
        public String getConnectorKind() {
                return SBMConnectorPlugin.CONNECTOR_KIND;
        }

        @Override
        protected Validator getValidator(TaskRepository repository) {
                return new SBMConnectionValidator(repository);
        }

        @Override
        protected boolean isValidUrl(String url) {
                if (url.contains("<server>"))
                        return false;
                return true;
        }

        @Override
        protected void applyValidatorResult(Validator validator) {
                if (((SBMConnectionValidator) validator).getResult() != null) {
                        setMessage("Validate first!", IMessageProvider.ERROR);
                        setNeedsValidation(true);
                } else {
                        regenerateTables();
                }
        }

        private void regenerateTables() {
                try {
                        List<String> tables = SBMRepositoryConnector.getClient(
                                        createTaskRepository()).getPrimaryTables();
                        for (String table_name : tables) {
                                table.add(table_name);
                                if (solutionTable.equals(table_name)) {
                                        table.setText(table_name);
                                }
                        }
                } catch (Exception e) {
                        // it was not possible to read the list of tables
                        // from the server, so just restore saved settings
                        table.add(solutionTable);
                        table.setText(solutionTable);
                }
                try {
                SBMField[] fields = SBMRepositoryConnector.getClient(
                                createTaskRepository()).getFields(table.getText()).toArray(
                                new SBMField[0]);
                                fillList(list, fields);
                } catch (Exception e) {
                        // it was not possible to read the fields for the given table
                        // do nothing
                }

        }

        protected class SBMConnectionValidator extends Validator {
                private TaskRepository repository;
                private String result = null;

                protected SBMConnectionValidator(TaskRepository repository) {
                        this.repository = repository;
                }

                @Override
                public void run(IProgressMonitor monitor) throws CoreException {
                        monitor.beginTask("Prüfe Verbindung", IProgressMonitor.UNKNOWN);
                        SBMClient client = new SBMClient(repository);
                        if (!client.canAuthenticate()) {
                                result = "Verbindung fehlgeschlagen";
                        } else {
                                result = null;
                        }
                        monitor.done();
                }

                public String getResult() {
                        return result;
                }
        }

        private void fillList(Table list, SBMField[] fields) {
                list.removeAll();
                for (int i = 0; i < fields.length; i++) {
                        TableItem item = new TableItem(list, 0);
                        item.setText(0, "");
                        item.setText(1, fields[i].getLabel());
                        item.setText(2, fields[i].getName());
                        item.setText(3, fields[i].getType().getValue().replaceFirst(
                                        "FLDTYPE-", ""));
                        try {
                                // auto-select all system fields
                                SBMSystemFields.valueOf(fields[i].getName());
                                item.setChecked(true);
                        } catch (IllegalArgumentException e) {
                                // do nothing
                        }
                        if (tableFieldList.contains(","+fields[i].getName()+",")) {
                                // item has been checked before
                                // so check it
                                item.setChecked(true);
                        }
                }

                for (TableColumn c : list.getColumns()) {
                        c.pack();
                }
        }

        @Override
        protected void createAdditionalControls(Composite arg0) {
                // TODO Auto-generated method stub

        }

}