Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, 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-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * Contributor(s):
  *
  * The Original Software is NetBeans. The Initial Developer of the Original
  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
  * Microsystems, Inc. All Rights Reserved.
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  */
 
 
 package org.netbeans.modules.i18n.wizard;
 
 
 import java.util.Map;
 
 
Second panel of I18N Wizard.

Author(s):
Peter Zavadsky
Marian Petras
See also:
ResourceWizardPanel.Panel
 
final class ResourceWizardPanel extends JPanel {

    
Local copy of i18n wizard data.
    private final Map<DataObject,SourceDatasourceMap = Util.createWizardSourceMap();

    
Table model for resourcesTable.
    private final ResourceTableModel tableModel = new ResourceTableModel();

    
This component panel wizard descriptor.

    private final Panel descPanel;
    private final boolean testMode;
    
    
Creates new form SourceChooserPanel.
    private ResourceWizardPanel(Panel descPanelboolean testMode) {
        this. = descPanel;
        this. = testMode;
        
        initComponents();        
        
        initTable();
        
        initAccesibility();
    }

    
    
Getter for resources property.
    public Map<DataObject,SourceDatagetSourceMap() {
        return ;
    }
    
    
Setter for resources property.
    public void setSourceMap(Map<DataObject,SourceDatasourceMap) {
            this..clear();
            this..putAll(sourceMap);
        
       
        .fireStateChanged();
    }
    
    private String getPanelDescription() {
        if ( == false) {
            return Util.getString("MSG_ResourcePanel_desc");
        } else {
            return Util.getString("MSG_ResourcePanel_test_desc");
        }
    }
    
    
Inits table component.
    private void initTable() {
            @Override
            public Component getTableCellRendererComponent(JTable tableObject value,
                boolean isSelectedboolean hasFocusint rowint column) {
                    
                JLabel label = (JLabel)super.getTableCellRendererComponent(tablevalueisSelectedhasFocusrowcolumn);
                DataObject dataObject = (DataObject)value;
                if(dataObject != null) {                    
                    String name = "N/A";
                    if (column == 0) {
                        // name for the first column, from sources
                        ClassPath cp = ClassPath.getClassPath(dataObject.getPrimaryFile(), . );                    
                        name = cp.getResourceNamedataObject.getPrimaryFile(), '.'false );
                    } else {
                        // name for resource bundle, from execution,
                        // but the reference file must be the
                        // corresponding source
                        DataObject dob = (DataObject).getValueAt(row, 0);
                        name = Util.getResourceName(dob.getPrimaryFile(), dataObject.getPrimaryFile(), '.'false);
                    }
                    label.setText(name); // NOI18N
                    label.setIcon(new ImageIcon(dataObject.getNodeDelegate().getIcon(.)));
                } else {
                    label.setText(""); // NOI18N
                    label.setIcon(null);
                }
                
                return label;
            }
        });
        Component cellSample = new DefaultTableCellRenderer()
                               .getTableCellRendererComponent(
                                    //table
                                    "N/A",          //value             //NOI18N
                                    false,          //isSelected
                                    false,          //hasFocus
                                    0, 0);          //row, column
        int cellHeight = cellSample.getPreferredSize().;
        int rowHeight = cellHeight + .getRowMargin();
        .setRowHeight(Math.max(16, rowHeight));
            public void valueChanged(ListSelectionEvent evt) {
            }
        });
        
    }
    
    
    private void initAccesibility() {        
        .getAccessibleContext().setAccessibleDescription(Util.getString("ACS_CTL_SelectResource"));//NOI18N
        .getAccessibleContext().setAccessibleDescription(Util.getString("ACS_CTL_SelectResourceAll"));//NOI18N
        .getAccessibleContext().setAccessibleDescription(Util.getString("ACSD_resourcesTable"));//NOI18N
        .getAccessibleContext().setAccessibleName(Util.getString("ACSN_resourcesTable"));//NOI18N
    }
    
    
This method is called from within the constructor to initialize the form.
    // <editor-fold defaultstate="collapsed" desc="UI initialization code">
    private void initComponents() {
         = new JTable();
         = new JButton();
        Mnemonics.setLocalizedText(
                ,
                Util.getString("CTL_SelectResourceAll"));               //NOI18N
            public void actionPerformed(ActionEvent evt) {
                addAllButtonActionPerformed(evt);
            }
        });
         = new JButton();
        Mnemonics.setLocalizedText(
                ,
                Util.getString("CTL_SelectResource"));                  //NOI18N
            public void actionPerformed(ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        Util.layoutSelectResourcePanel(
                this,
                getPanelDescription(),
                Util.getString("LBL_SelectedResources"),            //NOI18N
                ,
                ,
                );
    }// </editor-fold>
    private void addButtonActionPerformed(ActionEvent evt) {
        DataObject resource = selectResource();
        
        if (resource == null) {
            return;
        }
        int[] selectedRows = .getSelectedRows();
        // Feed data.
        for(int i=0; i<selectedRows.lengthi++) {
            DataObject dataObject = (DataObject.getValueAt(selectedRows[i], 0);
            .put(dataObjectnew SourceData(resource));
            
            .fireTableCellUpdated(selectedRows[i], 1);
        }
        .fireStateChanged();
    }
    private void addAllButtonActionPerformed(ActionEvent evt) {
        DataObject resource = selectResource();
        
        if (resource == null) {
            return;
        }
        // Feed data.
        for (int i = 0; i < .getRowCount(); i++) {
            DataObject dataObject = (DataObject.getValueAt(i, 0);
            .put(dataObjectnew SourceData(resource));
            
            .fireTableCellUpdated(i, 1);
        }
        .fireStateChanged();
    }

    
Helper method. Gets user selected resource.
    private DataObject selectResource() {        
        FileObject fo = null;
        DataObject source = null;
        for (DataObject dobj : .keySet()) {
            fo = dobj.getPrimaryFile();            
            source = dobj;
        }
        // Get i18n support for this source.
        I18nSupport support = null;
        try {
            support = FactoryRegistry.getFactory(source.getClass()).create(source);
        } catch(IOException ioe) {
            ErrorManager.getDefault().notify(.ioe);            
        }
    
        ResourceHolder rh = support != null ? support.getResourceHolder() : null;
        
        DataObject template = null;
        try {
            template = rh != null ? rh.getTemplate(rh.getResourceClasses()[0]) : null;
        } catch (IOException ex) {
            ErrorManager.getDefault().notify(ex);
        }
            
        return SelectorUtils.selectOrCreateBundle(fotemplatenull);
    }
    
    // Variables declaration
    private JButton addAllButton;
    private JButton addButton;
    private JTextArea descTextArea;
    private JScrollPane jScrollPane1;
    private JLabel lblSelectedResources;
    private JTable resourcesTable;
    // End of variables declaration

    
Table model for this class.
    private class ResourceTableModel extends AbstractTableModel {
        
        
Constructor.
        public ResourceTableModel() {
        }
        
        
        
Implements superclass abstract method.
        public int getColumnCount() {
            return 2;
        }
        
        
Implemenst superclass abstract method.
        public int getRowCount() {
            return .size();
        }
        
        
Implements superclass abstract method.
        public Object getValueAt(int rowIndexint columnIndex) {
            if (columnIndex == 0) {
                return .keySet().toArray()[rowIndex];
            } else { 
                SourceData[] values = new SourceData[0];
                SourceData value = .values().toArray(values)[rowIndex];
                return value == null ? null : value.getResource();
            }
            
        }
        
        
Overrides superclass method.

Returns:
DataObject.class
        @Override
        public Class getColumnClass(int columnIndex) {
            return DataObject.class;
        }

        
Overrides superclass method.
        @Override
        public String getColumnName(int column) {
            String msgKey = (column == 0) ? "CTL_Source"                //NOI18N
                                          : "CTL_Resource";             //NOI18N
            return NbBundle.getMessage(ResourceWizardPanel.classmsgKey);
        }
    } // End of ResourceTableModel inner class.
    
    
    
WizardDescriptor.Panel used for ResourceChooserPanel.

See also:
I18nWizardDescriptorPanel
org.openide.WizardDescriptor.Panel
    public static class Panel extends I18nWizardDescriptor.Panel
            implements AsynchronousValidatingPanel<I18nWizardDescriptor.Settings> {
        private static final String CARD_GUI = "gui";                   //NOI18N
        private static final String CARD_PROGRESS = "progress";         //NOI18N

        
Cached component.
        private transient ResourceWizardPanel resourcePanel;
        
        
Indicates whether this panel is used in i18n test wizard or not.
        private final boolean testWizard;

        
        private volatile ProgressWizardPanel progressPanel;

        
Constructs Panel for i18n wizard.
        public Panel() {
            this(false);
        }

        
Constructs panel for i18n wizard or i18n test wizard.
        public Panel(boolean testWizard) {
            this. = testWizard;
        }
        
        
        
Gets component to display. Implements superclass abstract method.

Returns:
this instance
        protected Component createComponent() {
            JPanel panel = new JPanel(new CardLayout());
            // Accessibility
            panel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ResourceWizardPanel.class).getString("ACS_ResourceWizardPanel"));                 
            
            panel.putClientProperty(., Integer.valueOf(1));
            String msgKey =  ? "TXT_SelectTestResource"       //NOI18N
                                       : "TXT_SelectResource";          //NOI18N
            panel.setName(NbBundle.getMessage(ResourceWizardPanel.classmsgKey));
            
            panel.add(getUI(), );
            return panel;
        }

        
Indicates if panel is valid. Overrides superclass method.
        @Override
        public boolean isValid() {
            return !getUI().getSourceMap().containsValue(null);
        }
        
        
Reads settings at the start when the panel comes to play. Overrides superclass method.
        @Override
        public void readSettings(I18nWizardDescriptor.Settings settings) {
	    super.readSettings(settings);
            getUI().setSourceMap(getMap());
            Container container = (ContainergetComponent();
            ((CardLayoutcontainer.getLayout()).show(container);
        }

        
Stores settings at the end of panel show. Overrides superclass abstract method.
        @Override
        public void storeSettings(I18nWizardDescriptor.Settings settings) {
	    super.storeSettings(settings);
            // Update sources.
            getMap().clear();
            getMap().putAll(getUI().getSourceMap());
        }
        
        
        public void prepareValidation() {
            assert EventQueue.isDispatchThread();
            if ( == null) {
                 = new ProgressWizardPanel(false);
            }
            showProgressPanel();
            .setMainText(NbBundle.getMessage(ResourceWizardPanel.class,
                                                          "TXT_Loading"));//NOI18N
            .setMainProgress(0);
        }
        public void validate() throws WizardValidationException {
            assert !EventQueue.isDispatchThread();
            // Do search.
            Map<DataObject,SourceDatasourceMap = getUI().getSourceMap();
            Iterator<Map.Entry<DataObject,SourceData>> sourceIterator
                    = sourceMap.entrySet().iterator();
            // For each source perform the task.
            final String prefixLoading
                    = NbBundle.getMessage(ResourceWizardPanel.class,
                                          "TXT_Loading")                //NOI18N
                      + ' ';
            final String prefixSearchingIn
                    = NbBundle.getMessage(ResourceWizardPanel.class,
                                          "TXT_SearchingIn")            //NOI18N
                      + ' ';
            for (int i = 0; sourceIterator.hasNext(); i++) {
                Map.Entry<DataObject,SourceDataentry = sourceIterator.next();
                DataObject source = entry.getKey();
                FileObject fileObj = source.getPrimaryFile();
                String fileName = ClassPath.getClassPath(fileObj.)
                                  .getResourceName(fileObj'.'false);
                .setMainText(prefixLoading + fileName);
                // retrieve existing sourcedata -- will provide the resource for the new instance
                SourceData sourceData = entry.getValue();
                
                // prepare new sourcedata
                // Get i18n support for this source.
                I18nSupport support = sourceData.getSupport();
                if (support == null) {
                    try {
                        support = FactoryRegistry.getFactory(source.getClass()).create(source);
                    } catch(IOException ioe) {
                        ErrorManager.getDefault().notify(.ioe);
                        // Remove source from settings.
                        sourceIterator.remove();
                        continue;
                    }
                    sourceData = new SourceData(sourceData.getResource(), support);
                    entry.setValue(sourceData);
                }
                
                .setMainText(prefixSearchingIn + fileName);
                
                HardCodedString[] foundStrings;
                
                if () {
                    // Find all i18n-zied hard coded strings in the source.
                    foundStrings = support.getFinder().findAllI18nStrings();
                } else {
                    // Find all non-i18-ized hard coded strings in the source.
                    foundStrings = support.getFinder().findAllHardCodedStrings();
                }
                if (foundStrings == null) {
                    // Set empty map.
                    sourceData.setStringMap(Collections.<HardCodedString,I18nString>emptyMap());
                    continue;
                }
                Map<HardCodedString,I18nStringmap
                        = new HashMap<HardCodedString,I18nString>(foundStrings.length); 
                // Put <hard coded string, i18n-string> pairs into map.
                for (HardCodedString hcString : foundStrings) {
                    if ( && support.getResourceHolder().getValueForKey(hcString.getText())
                                      != null) {
                        continue;
                    }
                        map.put(hcStringsupport.getDefaultI18nString(hcString));
                }
                .setMainProgress((int)((i+1)/(float)sourceMap.size() * 100));
                sourceData.setStringMap(map);
            } // End of outer for.
        }

        
Helper method. Places progress panel for monitoring search.
        private void showProgressPanel(ProgressWizardPanel progressPanel) {
            Container container = (ContainergetComponent();
            container.add(progressPanel);
            ((CardLayoutcontainer.getLayout()).show(container);
        }
        
        
Gets help. Implements superclass abstract method.
        public HelpCtx getHelp() {
            return new HelpCtx( ? .
                                          : .);
        }
        
        private synchronized ResourceWizardPanel getUI() {
            if ( == null) {
                 = new ResourceWizardPanel(this);
            }
            return ;
        }
    } // End of nested Panel class.
    
New to GrepCode? Check out our FAQ X