/**
* Project Wonderland
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., All Rights Reserved
*
* Redistributions in source code form must reproduce the above
* copyright and this condition.
*
* The contents of this file are subject to the GNU General Public
* License, Version 2 (the "License"); you may not use this file
* except in compliance with the License. A copy of the License is
* available at http://www.opensource.org/licenses/gpl-license.php.
*
* Sun designates this particular file as subject to the "Classpath"
* exception as provided by Sun in the License file that accompanied
* this code.
*/
package org.jdesktop.wonderland.modules.artimport.client.jme;
import java.awt.Component;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.jdesktop.wonderland.client.jme.artimport.ImportedModel;
import org.jdesktop.wonderland.modules.artimport.client.jme.LoaderWarningsHandler.ModelErrors;
/**
*
* Panel for displaying warning generated by the model loaders.
*
* @author paulby
*/
public class LoaderWarningsPanel extends javax.swing.JPanel {
private LoaderWarningsHandler handler;
private ImportedModel currentSelection=null;
/** Creates new form LoaderWarningsPanel */
LoaderWarningsPanel(LoaderWarningsHandler handler) {
this.handler = handler;
initComponents();
modelList.setCellRenderer(new MyCellRenderer());
}
void addModel(ImportedModel model) {
((DefaultListModel)modelList.getModel()).addElement(model);
}
void addError(ImportedModel model, String errorMsg) {
if (model==currentSelection) {
warningTextArea.append(errorMsg+"\n");
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
modelScrollPane = new javax.swing.JScrollPane();
modelList = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
warningScrollPane = new javax.swing.JScrollPane();
warningTextArea = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
clearAllB = new javax.swing.JButton();
setLayout(new java.awt.BorderLayout());
jPanel1.setLayout(new java.awt.BorderLayout());
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/jdesktop/wonderland/modules/artimport/client/jme/resources/Bundle"); // NOI18N
jLabel1.setText(bundle.getString("Models")); // NOI18N
jPanel1.add(jLabel1, java.awt.BorderLayout.NORTH);
modelList.setModel(new DefaultListModel());
modelList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
modelList.setFixedCellWidth(120);
modelList.setMaximumSize(new java.awt.Dimension(60, 85));
modelList.setMinimumSize(new java.awt.Dimension(60, 85));
modelList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
modelListValueChanged(evt);
}
});
modelScrollPane.setViewportView(modelList);
jPanel1.add(modelScrollPane, java.awt.BorderLayout.CENTER);
jSplitPane1.setLeftComponent(jPanel1);
jPanel2.setLayout(new java.awt.BorderLayout());
warningTextArea.setColumns(20);
warningTextArea.setEditable(false);
warningTextArea.setRows(5);
warningScrollPane.setViewportView(warningTextArea);
jPanel2.add(warningScrollPane, java.awt.BorderLayout.CENTER);
jLabel2.setText(bundle.getString("LOADER_WARNINGS")); // NOI18N
jPanel2.add(jLabel2, java.awt.BorderLayout.PAGE_START);
jSplitPane1.setRightComponent(jPanel2);
add(jSplitPane1, java.awt.BorderLayout.CENTER);
clearAllB.setText(bundle.getString("CLEAR_ALL")); // NOI18N
clearAllB.setToolTipText(bundle.getString("CLEAR_ALL_THE_WARNINGS_FOR_ALL_MODELS")); // NOI18N
clearAllB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearAllBActionPerformed(evt);
}
});
jPanel3.add(clearAllB);
add(jPanel3, java.awt.BorderLayout.SOUTH);
}// </editor-fold>//GEN-END:initComponents
private void modelListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_modelListValueChanged
ImportedModel selection = (ImportedModel) modelList.getSelectedValue();
Document doc = warningTextArea.getDocument();
try {
doc.remove(0, doc.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(LoaderWarningsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
if (selection!=null) {
ModelErrors me = handler.getModelErrors(selection);
for(String e : me.getErrors()) {
warningTextArea.append(e+"\n");
}
}
currentSelection = selection;
}//GEN-LAST:event_modelListValueChanged
private void clearAllBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearAllBActionPerformed
((DefaultListModel)modelList.getModel()).clear();
Document doc = warningTextArea.getDocument();
try {
doc.remove(0, doc.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(LoaderWarningsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
handler.clearAll();
}//GEN-LAST:event_clearAllBActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton clearAllB;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JList modelList;
private javax.swing.JScrollPane modelScrollPane;
private javax.swing.JScrollPane warningScrollPane;
private javax.swing.JTextArea warningTextArea;
// End of variables declaration//GEN-END:variables
class MyCellRenderer extends JLabel implements ListCellRenderer {
// This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called.
public Component getListCellRendererComponent(
JList list, // the list
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // does the cell have focus
{
String s = trimFilename(((ImportedModel)value).getOriginalURL());
setText(s);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
/**
* Return just the filename of this url, without the path
* @param url
* @return
*/
String trimFilename(URL url) {
String s = url.getPath();
return s.substring(s.lastIndexOf('/')+1, s.length());
}
}
}