001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * Created on May 6, 2004
043: *
044: * @todo To change the template for this generated file go to
045: * Window - Preferences - Java - Code Generation - Code and Comments
046: */
047: package org.netbeans.modules.visualweb.insync;
048:
049: import java.awt.Dialog;
050:
051: import org.openide.DialogDescriptor;
052: import org.openide.DialogDisplayer;
053: import org.openide.NotifyDescriptor;
054: import org.openide.awt.StatusDisplayer;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.NbBundle;
057:
058: import com.sun.rave.designtime.DisplayAction;
059: import com.sun.rave.designtime.Result;
060: import com.sun.rave.designtime.ResultMessage;
061: import com.sun.rave.designtime.Customizer2;
062: import com.sun.rave.designtime.CustomizerResult;
063: import org.netbeans.modules.visualweb.insync.models.FacesModel;
064:
065: /**
066: * @author tor
067: *
068: * @todo To change the template for this generated type comment go to
069: * Window - Preferences - Java - Code Generation - Code and Comments
070: */
071: public class ResultHandler {
072:
073: /**
074: * Handle the result object; if necessary, pop up a dialog, etc.
075: */
076: public static void handleResult(Result r, FacesModel model) {
077: if (r == null) // success - do nothing
078: return;
079:
080: ResultMessage[] messages = r.getMessages();
081: if (messages != null) {
082:
083: // Decide if we need to show a dialog
084: boolean hasDialog = false;
085: DisplayAction[] options = r.getResultOptions();
086: if (options != null && options.length > 0) {
087: hasDialog = true;
088: } else {
089: for (int i = 0; i < messages.length; i++) {
090: if (messages[i].getMessageType() == ResultMessage.TYPE_CRITICAL) {
091: hasDialog = true;
092: break;
093: }
094: }
095: }
096:
097: if (!hasDialog) {
098: // Status message: build up compound string
099: StringBuffer sb = new StringBuffer(200);
100: boolean hasWarning = false;
101: for (int i = 0; i < messages.length; i++) {
102: ResultMessage m = messages[i];
103: if (m.getMessageType() == ResultMessage.TYPE_WARNING)
104: hasWarning = true;
105:
106: sb.append(m.getDescription());
107: if (i < messages.length - 1)
108: sb.append(' ').append('/').append(' ');
109: }
110: // This block only in place to get rid of warning that hasWarning is not read
111: if (hasWarning) {
112: }
113: StatusDisplayer.getDefault().setStatusText(
114: sb.toString());
115: } else {
116: // Dialog: build up component html string for option pane
117: StringBuffer sb = new StringBuffer(400);
118: sb.append("<html><body>");
119: boolean hasCritical = false;
120: boolean hasWarning = false;
121: String title = r.getDialogTitle();
122: if (title == null || title.length() == 0) {
123: title = NbBundle.getMessage(ResultHandler.class,
124: "HandlerTitle");
125: }
126: String help = r.getDialogHelpKey();
127: HelpCtx helpCtx = null;
128: if ((help != null) && (help.length() > 0)) {
129: helpCtx = new HelpCtx(help);
130: }
131: for (int i = 0; i < messages.length; i++) {
132: ResultMessage m = messages[i];
133: if (m.getMessageType() == ResultMessage.TYPE_CRITICAL)
134: hasCritical = true;
135: else if (m.getMessageType() == ResultMessage.TYPE_WARNING)
136: hasWarning = true;
137:
138: if (m.getDisplayName() != null) {
139: sb.append("<b>");
140: sb.append(m.getDisplayName());
141: sb.append("</b>");
142: sb.append("<br>");
143: }
144: if (m.getDescription() != null) {
145: sb.append(m.getDescription());
146: if (i < messages.length - 1)
147: sb.append("<br>").append("<br>");
148: } else {
149: sb.append("<br>");
150: }
151: }
152: sb.append("</body></html>");
153:
154: Object[] os = null;
155: Object def = null;
156: if (options != null && options.length > 0) {
157: os = new Object[options.length];
158: for (int i = 0; i < options.length; i++)
159: os[i] = options[i].getDisplayName();
160:
161: if (options.length > 1)
162: def = os[0];
163: } else {
164: // No options provided - so make a single OK button.
165: // Otherwise the default DialogDescriptor code will kick
166: // in and make an OK_CANCEL dialog, and there is no
167: // just-OK-button type.
168: def = new javax.swing.JButton(NbBundle.getMessage(
169: ResultHandler.class, "OK"));
170: os = new Object[] { def };
171: }
172: DialogDescriptor dlg = new DialogDescriptor(sb
173: .toString(), title, true, os, def,
174: DialogDescriptor.DEFAULT_ALIGN, helpCtx, null);
175:
176: if (hasCritical)
177: dlg.setMessageType(NotifyDescriptor.ERROR_MESSAGE);
178: else if (hasWarning)
179: dlg
180: .setMessageType(NotifyDescriptor.WARNING_MESSAGE);
181: else
182: dlg
183: .setMessageType(NotifyDescriptor.INFORMATION_MESSAGE);
184:
185: Dialog dialog = DialogDisplayer.getDefault()
186: .createDialog(dlg);
187: dialog.setModal(true);
188: dialog.show();
189: Object chosen = dlg.getValue();
190: if (options != null && options.length > 0) {
191: for (int i = 0; i < options.length; i++) {
192: if (chosen == options[i].getDisplayName()) {
193: Result r2 = options[i].invoke();
194: handleResult(r2, model); // recurse
195: break;
196: }
197: }
198: }
199: }
200: }
201:
202: if (r instanceof CustomizerResult) {
203: CustomizerResult cr = (CustomizerResult) r;
204: Customizer2 c = cr.getCustomizer();
205: CustomizerDisplayer cd = new CustomizerDisplayer(cr
206: .getCustomizeBean(), c, c.getHelpKey(), model);
207: cd.show();
208: return;
209: }
210: }
211:
212: }
|