/* * The contents of this file are subject to the terms of the Common Development * and Distribution License (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at http://www.netbeans.org/cddl.html * or http://www.netbeans.org/cddl.txt. * * When distributing Covered Code, include this CDDL Header Notice in each file * and include the License file at http://www.netbeans.org/cddl.txt. * If applicable, add the following below the CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * The Original Software is NetBeans. The Initial Developer of the Original * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.j2ee.common.ui; import java.awt.Dialog; import java.util.Set; import javax.swing.JButton; import org.netbeans.api.project.Project; import org.netbeans.modules.j2ee.deployment.common.api.Datasource; import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.NbBundle; /** * Support for managing broken/missing Databases. * * PLEASE NOTE! This is just a temporary solution. BrokenReferencesSupport from * the java project support currently does not allow to plug in a check for missing * Databases. Once BrokenReferencesSupport will support it, this class should be * removed. */ public class BrokenDatabaseSupport { /** Last time in ms when the Broken References alert was shown. */ private static long brokenAlertLastTime = 0; /** Is Broken References alert shown now? */ private static boolean brokenAlertShown = false; /** Timeout within which request to show alert will be ignored. */ private static int BROKEN_ALERT_TIMEOUT = 1000; private static DatabaseConnectionResolver databaseConnectionResolver = new DatabaseConnectionResolver(); private static Set brokenDatasources = null; private BrokenDatabaseSupport() {} /** * Checks whether the project has a broken/missing Database problem. * * @param project * @return true server instance of the specified id doesn't exist */ public static boolean isBroken(Project project) { brokenDatasources = databaseConnectionResolver.getBrokenDataSources(project); if (brokenDatasources == null) return false; else return !brokenDatasources.isEmpty(); } /** * Returns set of broken datasources * * @param project * @return Set returns a set of data sources without corresponding database connections */ public static Set getBrokenDatasources(Project project) { brokenDatasources = databaseConnectionResolver.getBrokenDataSources(project); return brokenDatasources; } /** * Returns a broken datasource * * @param project * @param item this is the datasource item selected in the Resolve Data Sources dialog * @return Set returns a set of data sources without corresponding database connections */ public static Datasource getBrokenDatasource(Project project, String item) { Datasource brokenDatasource = databaseConnectionResolver.getBrokenDataSource(project, item); return brokenDatasource; } /** * Shows UI customizer which gives users chance to fix encountered problems, * i.e. choose appropriate data source. * * @param project * @return selected database connection. Might be null. */ public static String selectDatasource(final Project project) { return NoSelectedDatabaseConnectionWarning.selectDatabaseDialog( NbBundle.getMessage(BrokenDatabaseSupport.class, "LBL_Resolve_Missing_Dataources_Title"), NbBundle.getMessage(BrokenDatabaseSupport.class, "ACSD_Resolve_Missing_Datasources"), brokenDatasources, project); // NOI18N } /** * Refresh list of broken datasources; dismiss dialog if no broken datasources remaining * */ public static void disposeIfEmpty() { Dialog dlg = NoSelectedDatabaseConnectionWarning.getNoSelectedDatabaseConnectionWarningDialog(); if (brokenDatasources.isEmpty()) { dlg.dispose(); } } /** * Show alert message box informing user that a project has missing * database connections. This method can be safely called from any thread, e.g. during * the project opening, and it will take care about showing message box only * once for several subsequent calls during a timeout. * The alert box has also "show this warning again" check box. */ public static synchronized void showAlert() { brokenAlertShown = true; // how to do check for an enabled checkbox for Datasources ? // if (Boolean.getBoolean("j2eeserver.no.server.instance.check")) { // return; // } try { BrokenDatabaseAlertPanel alert = new BrokenDatabaseAlertPanel(); JButton close = new JButton( NbBundle.getMessage(BrokenDatabaseSupport.class, "LBL_BrokenDatasourcesCustomizer_Close")); close.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(BrokenDatabaseSupport.class, "ACSD_BrokenDatasourcesCustomizer_Close")); DialogDescriptor dd = new DialogDescriptor( alert, NbBundle.getMessage(BrokenDatabaseAlertPanel.class, "MSG_Broken_Datasources_Title"), true, new Object[] {close}, close, DialogDescriptor.DEFAULT_ALIGN, null, null); dd.setMessageType(DialogDescriptor.WARNING_MESSAGE); Dialog dlg = DialogDisplayer.getDefault().createDialog(dd); dlg.setVisible(true); } finally { synchronized (BrokenDatabaseSupport.class) { brokenAlertLastTime = System.currentTimeMillis(); brokenAlertShown = false; } } } public static boolean addDatabaseConnection(Datasource brokenDatasource, String item) { DatabaseConnectionResolver databaseConnectionResolver = new DatabaseConnectionResolver(); return databaseConnectionResolver.addDatabaseConnection(brokenDatasource, item); } }