FileDocCategorySizeDatePackage
MimePanel.javaAPI DocJMF 2.1.1e13420Mon May 12 12:20:30 BST 2003jmapps.registry

MimePanel

public class MimePanel extends JMPanel implements ActionListener, ItemListener, TextListener

Fields Summary
private Hashtable
htMimeTypes
private Button
buttonRestore
private Button
buttonCommit
private Button
buttonAdd
private Button
buttonRemove
private List
listBoxMimeType
private TextField
textMimeType
private List
listBoxMimeExt
private TextField
textMimeExt
Constructors Summary
public MimePanel()

        super ();

        init ();
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent event)

        String          strCmd;
        Object          objSource;

        objSource = event.getSource ();
        strCmd = event.getActionCommand ();
        if ( strCmd.equals(buttonRestore.getActionCommand()) )
            restore ();
        if ( strCmd.equals(buttonCommit.getActionCommand()) )
            MimeManager.commit();
        else if ( objSource == buttonAdd )
            addExt ();
        else if ( objSource == buttonRemove )
            removeExt ();

        updateButtons ();
    
private voidaddExt()

        String          strType;
        String          strExt;
        Vector          vectorExt;
        boolean         boolResult = true;

        strExt = textMimeExt.getText().trim();
        if ( strExt.length() < 1 )
            return;
        strType = textMimeType.getText().trim();
        if ( strType.length() < 1 )
            return;

        if ( !(htMimeTypes.containsKey(strType)) )
            addType ();
        if ( !(htMimeTypes.containsKey(strType)) )
            return;

        vectorExt = (Vector) htMimeTypes.get ( strType );
        if ( !vectorExt.contains(strExt) ) {
            boolResult = MimeManager.addMimeType ( strExt, strType );
            if ( boolResult == true ) {
                vectorExt.addElement ( strExt );
                listBoxMimeExt.add ( strExt );
            }
            else {
                MessageDialog.createErrorDialog ( new Frame(),
                                JMFI18N.getResource("jmfregistry.appname"),
                                JMFI18N.getResource("jmfregistry.error.additem") );
            }
        }

        if ( boolResult == true )
            selectItem ( listBoxMimeExt, strExt );
    
private voidaddType()

        String          strType;
        Vector          vectorExt;

        strType = textMimeType.getText().trim();
        if ( strType.length() < 1 )
            return;

        if ( !htMimeTypes.containsKey(strType) ) {
            vectorExt = new Vector ();
            htMimeTypes.put ( strType, vectorExt );
            listBoxMimeType.add ( strType );
        }

        selectItem ( listBoxMimeType, strType );
        fillExtList ();
    
private java.awt.PanelcreatePanelButtons()

        Panel   panelButtons;
        Panel   panel;

        panelButtons = new Panel ( new FlowLayout(FlowLayout.LEFT) );

        panel = new Panel ( new GridLayout(0,1,12,12) );
        panelButtons.add ( panel );

        buttonAdd = new Button ( JMFI18N.getResource("jmfregistry.vector.add") );
        buttonAdd.addActionListener ( this );
        panel.add ( buttonAdd );

        buttonRemove = new Button ( JMFI18N.getResource("jmfregistry.vector.remove") );
        buttonRemove.addActionListener ( this );
        panel.add ( buttonRemove );

        buttonRestore = new Button ( JMFI18N.getResource("jmfregistry.vector.restore") );
        buttonRestore.addActionListener ( this );
//        panel.add ( buttonRestore );

        buttonCommit = new Button ( JMFI18N.getResource("jmfregistry.vector.commit") );
        buttonCommit.addActionListener ( this );
        panel.add ( buttonCommit );

        return ( panelButtons );
    
private java.awt.PanelcreatePanelMimeExt()

        Label   label;
        Panel   panel;
        Panel   panelTemp;


        panel = new Panel ( new BorderLayout(6,6) );

        panelTemp = new Panel ( new BorderLayout(6,6) );
        panel.add ( panelTemp, BorderLayout.NORTH );

        label = new Label ( JMFI18N.getResource("jmfregistry.mime.ext.label") );
        panelTemp.add ( label, BorderLayout.NORTH );

        textMimeExt = new TextField ();
        textMimeExt.addTextListener ( this );
        panelTemp.add ( textMimeExt, BorderLayout.CENTER );

        listBoxMimeExt = new List ();
        listBoxMimeExt.addItemListener ( this );
        panel.add ( listBoxMimeExt, BorderLayout.CENTER );

        return ( panel );
    
private java.awt.PanelcreatePanelMimeTypes()

        Panel   panel;
        Panel   panelTemp;
        Label   label;


        panel = new Panel ( new BorderLayout(6,6) );

        panelTemp = new Panel ( new BorderLayout(6,6) );
        panel.add ( panelTemp, BorderLayout.NORTH );

        label = new Label ( JMFI18N.getResource("jmfregistry.mime.type.label") );
        panelTemp.add ( label, BorderLayout.NORTH );

        textMimeType = new TextField ();
        textMimeType.addTextListener ( this );
        panelTemp.add ( textMimeType, BorderLayout.CENTER );

        listBoxMimeType = new List ();
        listBoxMimeType.addItemListener ( this );
        panel.add ( listBoxMimeType, BorderLayout.CENTER );

        return ( panel );
    
private voidfillExtList()

        int             i;
        int             nCount;
        String          strType;
        Vector          vectorExt;
        Object          objExt;

        listBoxMimeExt.removeAll ();
        strType = listBoxMimeType.getSelectedItem ();
        if ( strType == null )
            return;

        vectorExt = (Vector) htMimeTypes.get ( strType );
        nCount = vectorExt.size ();
        for ( i = 0;  i < nCount;  i++ ) {
            objExt = vectorExt.elementAt ( i );
            listBoxMimeExt.add ( objExt.toString() );
        }

//        if ( listBoxMimeExt.getItemCount() > 0 )
//            listBoxMimeExt.select ( 0 );
    
private voidfillTypeList()

        Enumeration     enumKeys;
        Object          objType;

        listBoxMimeType.removeAll ();
        enumKeys = htMimeTypes.keys();
        while ( enumKeys.hasMoreElements() ) {
            objType = enumKeys.nextElement ();
            listBoxMimeType.add ( objType.toString() );
        }

//        if ( listBoxMimeType.getItemCount() > 0 )
//            listBoxMimeType.select ( 0 );
//        fillExtList ();
    
private intgetItemIndex(java.awt.List listBox, java.lang.String strItemSel)

        int     i;
        int     nCount;
        String  strItem;

        nCount = listBox.getItemCount ();
        for ( i = 0;  i < nCount;  i++ ) {
            strItem = listBox.getItem ( i );
            if ( strItem.equals(strItemSel) )
                return ( i );
        }
        return ( -1 );
    
private voidinit()

        Panel   panel;
        Panel   panelData;

        this.setLayout ( new BorderLayout(6,6) );

        panel = createPanelButtons ();
        this.add ( panel, BorderLayout.EAST );

        panelData = new Panel ( new GridLayout(1,0,12,12) );
        this.add ( panelData, BorderLayout.CENTER );

        panel = createPanelMimeTypes ();
        panelData.add ( panel );

        panel = createPanelMimeExt ();
        panelData.add ( panel );

        restore ();
        updateButtons ();
    
public voiditemStateChanged(java.awt.event.ItemEvent event)

        Object      objSource;
        String      strValue;

        objSource = event.getSource ();
        if ( objSource == listBoxMimeType ) {
            strValue = listBoxMimeType.getSelectedItem ();
            textMimeType.setText ( strValue );
            fillExtList ();
        }
        else if ( objSource == listBoxMimeExt ) {
            strValue = listBoxMimeExt.getSelectedItem ();
            textMimeExt.setText ( strValue );
        }
        updateButtons ();
    
private voidremoveExt()

        int             i;
        String          strType;
        String          strExt;
        Vector          vectorExt;
        boolean         boolResult = true;

        strExt = textMimeExt.getText().trim();
        if ( strExt.length() < 1 )
            return;
        strType = textMimeType.getText().trim();
        if ( strType.length() < 1 )
            return;

        if ( !(htMimeTypes.containsKey(strType)) )
            return;

        vectorExt = (Vector) htMimeTypes.get ( strType );
        if ( !vectorExt.contains(strExt) )
            return;

        boolResult = MimeManager.removeMimeType ( strExt );
        if ( boolResult == true ) {
            vectorExt.removeElement ( strExt );
            i = getItemIndex ( listBoxMimeExt, strExt );
            listBoxMimeExt.remove ( strExt );
//            if ( i >= listBoxMimeExt.getItemCount() )
//                i = listBoxMimeExt.getItemCount() - 1;
//            if ( i >= 0 )
//                listBoxMimeExt.select ( i );
            if ( vectorExt.isEmpty() )
                removeType ();
        }
        else {
            MessageDialog.createErrorDialog ( new Frame(),
                        JMFI18N.getResource("jmfregistry.appname"),
                        JMFI18N.getResource("jmfregistry.error.removeitem") );
        }
    
private voidremoveType()

        int             i;
        int             nCount;
        String          strType;
        String          strExt;
        Vector          vectorExt;
        boolean         boolResult = true;

        strType = textMimeType.getText().trim();
        if ( strType.length() < 1 )
            return;
        vectorExt = (Vector) htMimeTypes.get ( strType );

        nCount = vectorExt.size ();
        for ( i = nCount - 1;  i >= 0;  i-- ) {
            strExt = (String) vectorExt.elementAt ( i );
            boolResult = MimeManager.removeMimeType ( strExt );
            if ( boolResult == true ) {
                vectorExt.removeElement ( strExt );
                listBoxMimeExt.remove ( strExt );
            }
        }

        nCount = vectorExt.size ();
        if ( nCount > 0 ) {
            listBoxMimeExt.select ( 0 );
            MessageDialog.createErrorDialog ( new Frame(),
                                JMFI18N.getResource("jmfregistry.appname"),
                                JMFI18N.getResource("jmfregistry.error.removeitem") );
        }
        else {
            htMimeTypes.remove ( strType );
            i = getItemIndex ( listBoxMimeType, strType );
            listBoxMimeType.remove ( strType );
//            if ( i >= listBoxMimeType.getItemCount() )
//                i = listBoxMimeType.getItemCount() - 1;
//            if ( i >= 0 ) {
//                listBoxMimeType.select ( i );
//                fillExtList();
//            }
        }
    
private voidrestore()

        Hashtable       htMimeExt;
        Enumeration     enumKeys;
        Object          objExt;
        Object          objType;
        String          strExt;
        String          strType;
        Vector          vectorExt;

        htMimeExt = MimeManager.getMimeTable ();
        htMimeTypes = new Hashtable ();

        enumKeys = htMimeExt.keys ();
        while ( enumKeys.hasMoreElements() ) {
            objExt = enumKeys.nextElement ();
            objType = htMimeExt.get ( objExt );
            strExt = objExt.toString ();
            strType = objType.toString ();

            if ( htMimeTypes.containsKey(strType) ) {
                vectorExt = (Vector)htMimeTypes.get ( strType );
            }
            else {
                vectorExt = new Vector ();
                htMimeTypes.put ( strType, vectorExt );
            }

            if ( !vectorExt.contains(strExt) )
                vectorExt.addElement ( strExt );
        }
        fillTypeList ();
    
private voidselectItem(java.awt.List listBox, java.lang.String strItemSel)

        int     i;

        i = getItemIndex ( listBox, strItemSel );
        if ( i >= 0 )
            listBox.select ( i );
    
public voidtextValueChanged(java.awt.event.TextEvent event)

        updateButtons ();
    
private voidupdateButtons()

        String          strType;
        String          strExt;
        Vector          vectorExt;
        boolean         boolAddEnable;
        boolean         boolRemoveEnable;

        strExt = textMimeExt.getText().trim();
        strType = textMimeType.getText().trim();

        if ( strExt.length() > 1  &&  strType.length() > 1 ) {
            boolAddEnable = true;
            boolRemoveEnable = true;
        }
        else {
            boolAddEnable = false;
            boolRemoveEnable = false;
        }

        if ( htMimeTypes.containsKey(strType) ) {
            vectorExt = (Vector) htMimeTypes.get ( strType );
            if ( vectorExt.contains(strExt) )
                boolAddEnable = false;
            else
                boolRemoveEnable = false;
        }
        else {
            boolRemoveEnable = false;
        }

        buttonAdd.setEnabled ( boolAddEnable );
        buttonRemove.setEnabled ( boolRemoveEnable );