⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainmenu.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    {
        // define an listener for the ldif menu

        ActionListener ldifListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JMenuItem item = (JMenuItem)e.getSource();

                SmartTree activeTree = jxplorer.getActiveTree();
                boolean usingSearch = (activeTree == jxplorer.searchTree);

                if (item == fullExport)
                    ldifFullExport(activeTree, usingSearch);
                else if (item == subExport)
                    ldifSubExport(activeTree, usingSearch);
                else if (item == importFile)
                    importFile();
                else if (item == viewOffline)
                    viewOffline();

                jxplorer.repaint();
            }
        };

        // define the individual options.

        fullExport = setMenuItem(ldifMenu, ldifListener,
            new String[] {CBIntText.get("Export Full Tree"), "x", CBIntText.get("Export an LDIF file of the entire tree."), "E", ""} );

        subExport = setMenuItem(ldifMenu, ldifListener,
            new String[] {CBIntText.get("Export Subtree"),   "p", CBIntText.get("Export an LDIF file of the current subtree."), "E", ""} );

        importFile = setMenuItem(ldifMenu, ldifListener,
            new String[] {CBIntText.get("Import File"),      "I", CBIntText.get("Import an LDIF file into the directory."), "E", ""} );

        viewOffline = setMenuItem(ldifMenu, ldifListener,
            new String[] {CBIntText.get("View Offline"),     "w", CBIntText.get("View an LDIF file off-Line, without adding to a directory."), "E", ""} );

        ButtonRegister br = JXplorer.getButtonRegister();
        br.registerItem(br.LDIF, fullExport);
        br.registerItem(br.LDIF, subExport);
        br.registerItem(br.LDIF, importFile);
    }

    public void ldifFullExport(SmartTree activeTree, boolean usingSearch)
    {
        DN base = activeTree.getRootDN();
        DataSource datasource = activeTree.getDataSource();
        LdifExport export = new LdifExport(base, datasource, jxplorer.searchTree, usingSearch, jxplorer, HelpIDs.LDIF_EXPORT_TREE);
        export.setSize(360,120);
        export.setTitle(CBIntText.get("Export Full Tree"));
        CBUtility.center(export, jxplorer);
        export.setVisible(true);
    }

    public void ldifSubExport(SmartTree activeTree, boolean usingSearch)
    {
        DN base = activeTree.getCurrentDN();
        DataSource datasource = activeTree.getDataSource();
        LdifExport export = new LdifExport(base, datasource, jxplorer.searchTree, usingSearch, jxplorer, HelpIDs.LDIF_EXPORT_SUBTREE);
        export.setSize(360,120);
        export.setTitle(CBIntText.get("Export Subtree"));
        CBUtility.center(export, jxplorer);
        export.setVisible(true);
    }

    public void importFile()
    {
        DataSource datamodifier;
        if (jxplorer.workOffline)
            datamodifier = (DataSource)jxplorer.offlineBroker;
        else
        {
            if (jxplorer.jndiBroker.isActive())
                datamodifier = (DataSource)jxplorer.jndiBroker;
            else
            {
                CBUtility.error(jxplorer, "Error: Not Connected! (Did You Want to 'View Offline'?)");
                return;
            }
        }
        LdifImport imp = new LdifImport(datamodifier, jxplorer.mrTree, jxplorer, null);
    }

    public void viewOffline()
    {
        disconnect();
        jxplorer.setStatus("Working Offline");
        jxplorer.workOffline = true ;
        jxplorer.offlineBroker.clear();
        jxplorer.mrTree.registerDataSource(jxplorer.offlineBroker);
        jxplorer.mrTree.setRoot(new DN(SmartTree.NODATA));
        LdifImport imp = new LdifImport(jxplorer.offlineBroker, jxplorer.mrTree, jxplorer, null);

        // XXX This activates the rest of the LDIF menu, *but* it will leave the LDIF menu activated
        // XXX even if the view offline load fails for some reason :-(.  Not sure how to fix this
        // XXX problem... - CB

        ButtonRegister br = JXplorer.getButtonRegister();
        br.setItemEnabled(br.LDIF, true);

    }



   /**
    *	Sets up the search menu with menu items and listeners.  Dynamically adds search filter names to the
	*	menu.  If there is more than 15, these names are added to a scrollable list.  A search is conducted
	*	if a user selects one of these names.
	*	@param searchMenu the actual search menu that needs to be set up.
	*/

    protected void setupSearchMenu(JMenu searchMenu)
    {
        final SearchModel sm = new SearchModel();

		searchMenu.removeAll();

		ArrayList list = sm.getFilterNames(SearchModel.FULLNAMES);						//TE: get the full names of the saved search filters.
		Object names[] = list.toArray();
		Arrays.sort(names, new SearchModel.StringComparator());					//TE: sort the list alphabetically.

        ActionListener searchListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JMenuItem item = (JMenuItem)e.getSource();

				if (item == search)
				{
					tree = jxplorer.getActiveTree();
					tree.openSearch();											//TE: open the search window (via Smart Tree).
				}
				else if (item == deleteFilter)
				{
					if (sm.getFilterNames(SearchModel.ALLFILTERS).size()==0)
					{
						JOptionPane.showMessageDialog(jxplorer, CBIntText.get("There are no filters available to delete."), CBIntText.get("Nothing to Delete"), JOptionPane.INFORMATION_MESSAGE );
						return;
					}
					else
					{
						DeleteFilterGUI dfg = new DeleteFilterGUI(jxplorer);	//TE: open the delete filter dialog.
						dfg.setVisible(true);
					}
				}
				else if (item == attrList)
				{
					 ReturnAttributesDialog rad = new ReturnAttributesDialog(jxplorer);
					 rad.setVisible(true);
				}
				else
				{
					doSearch(item.getText(), ((myJMenuItem)item).getToolTipText());
				}

                jxplorer.repaint();
            }
        };

        search = setMenuItem(searchMenu, searchListener,
            new String[] {CBIntText.get("Search Dialog"),  "Ctrl+F", CBIntText.get("Search the directory."), "E", dirImages+"find.gif"} );

        deleteFilter = setMenuItem(searchMenu, searchListener,
            new String[] {CBIntText.get("Delete Filter"),  "", CBIntText.get("Delete an existing filter."), "E", dirImages+"delete.gif"} );

        attrList = setMenuItem(searchMenu, searchListener,
            new String[] {CBIntText.get("Return Attribute Lists"),  "", CBIntText.get("Opens a dialog that lets you manage the attributes that you wish to be returned in a search."), "E", dirImages+"return_attrs.gif"} );

        if(names.length > 0)
		    setMenuItem(searchMenu, searchListener, new String[] {"-", "", "", "", ""} );

        ButtonRegister br = JXplorer.getButtonRegister();
        br.registerItem(br.SEARCH, search);

		String[] searchValues = new String[names.length];
		String[] searchNames = new String[names.length];

		for(int i=0; i<names.length;i++)
		{
			String key = names[i].toString();
			searchNames[i] = key;
			searchValues[i] = sm.getLDAPFilter(searchNames[i]);
			searchNames[i] = searchNames[i].startsWith("JXFilter") ? searchNames[i].substring(9) : searchNames[i].substring(13);	//TE: cut the prefix off (e.g: either 'JXFilter' or 'JXTextFilter').
		}

		if (names.length>15)	//TE: add a scrollable list rather than a blown out menu!
		{
			searchMenu.add(getScrollList(searchNames, searchValues, searchMenu));
		}
		else
		{
			for(int i=0;i<names.length;i++)
			{
				myJMenuItem searchMenuItem = new myJMenuItem(searchNames[i]);
				searchMenuItem.name = searchValues[i];
				setMenuItem(searchMenu, searchMenuItem, searchListener, new String[] {"", "", searchValues[i], "E", ""});
			}
		}
    }



   /**
    *	Runs the search.  Uses the saved search params from the property file if there
	*	is any.
	*	@param name the name of the saved filter.
	*	@param filter the LDAP filter.
	*/

	public void doSearch(String name, String filter)
	{
        SearchModel sm = new SearchModel();

		//TE: base DN...
		String baseDN = sm.getValue(name+"."+SearchModel.BASEDN);
		DN dn = (baseDN==null) ? jxplorer.getActiveTree().getCurrentDN() : new DN(baseDN);

		//TE: search level (base object, one level, full subtree)...
		int searchLevel = 2;
		try
		{
			searchLevel = Integer.parseInt(sm.getValue(name+"."+SearchModel.SEARCHLEVEL));
		}
		catch(NumberFormatException e)
		{
			searchLevel = 2;
		}

		//TE: Return attributes...
		String retAttrs = sm.getValue(name+"."+SearchModel.RETATTRS);
		String[] retAttrsList = null;

		if (retAttrs == null)  // CB if it isn't specified, just return object class
		{
			retAttrsList = new String[] {"objectClass"};
		}
		else if (retAttrs.equalsIgnoreCase(ReturnAttributesDialog.DEFAULT_RETURN_ATTRS)) // CB to return all attributes, use the magic value 'null' (see jndi.search() methods)
		{
            retAttrs = null;
		}
        else  //TE: there is a list of return attributes so get it...
        {
            retAttrsList = ReturnAttributesDialog.getReturnAttributes(retAttrs);
			sm.openRetAttrDisplay(jxplorer, retAttrsList, (jxplorer.getSearchTree()).getDataSource());
        }

        // Get the alias options...
        String find = name+"."+SearchModel.FIND;
        String search = name+"."+SearchModel.SEARCH;

        String aliasOption = "always";

        if (search.equalsIgnoreCase("false") && find.equalsIgnoreCase("false"))
            aliasOption = "never";
        else if (search.equalsIgnoreCase("true") && find.equalsIgnoreCase("false"))
            aliasOption = "searching";
        else if (search.equalsIgnoreCase("false") && find.equalsIgnoreCase("true"))
            aliasOption = "finding";

        log.info("Setting search alias option to: ["+aliasOption+"]");
        JXplorer.setProperty("option.ldap.searchAliasBehaviour", aliasOption);

		//TE: run the search...
		SearchExecute.run(jxplorer.getSearchTree(),
							dn,
							filter,
							retAttrsList,
							searchLevel,
							jxplorer.getSearchBroker());

		//TE: go to results tab...
		jxplorer.getTreeTabPane().setSelectedComponent(jxplorer.getResultsPanel());
	}



   /**
    *	Enables or disables the search menu items (below the separator).
	*	@param state disabled if false, enabled if true.
	*/

	public void setSearchEnabled(boolean state)
	{
		if(searchMenu==null)
			return;

		int items = searchMenu.getItemCount();

		for(int i=0;i<items;i++)
		{
			JMenuItem temp = searchMenu.getItem(i);

			if(temp instanceof myJMenuItem)
				temp.setEnabled(state);
		}
		searchMenu.repaint();
	}



   /**
    *	Calls the set up for the search menu.  Used to dynamically update the filter names within the search menu.
	*/

	public void updateSearchMenu()
	{
		setupSearchMenu(searchMenu);
	}



   /**
    *	Sets up a scrollable list with given items and tooltips.  Adds a listener to each list item.
	*	@param items an array of things (usually strings that are added to the list.
	*	@param toolTips and array of strings used as the tooltips for the items (i.e. tooltip[x] goes with item[x]).
	*	@param menuType the menu that the listener is reponding to (either the search menu or the bookmark menu).
	*	@return the scroll pane with the list and listeners added.
	*/

	protected JScrollPane getScrollList(Object[] items, String[] toolTips, JMenu menuType)
	{
		final Object[] names = items;
		final String[] toolTps = toolTips;
		final JMenu type = menuType;

		final JList list = new JList(names)
		{
			public String getToolTipText(MouseEvent e)  						//TE: add a tool tip!
			{
				int index = locationToIndex(e.getPoint());
				if (-1 < index)
					return toolTps[index];
				else
					return null;
			}
		};
		list.setToolTipText("");
		list.setBackground(Color.lightGray);

		list.addListSelectionListener(new ListSelectionListener (){
			public void valueChanged(ListSelectionEvent e){
				if (e.getSource() == list && !e.getValueIsAdjusting())			//TE: listener registers mouse down and mouse release!  This is only to work on the mouse release!
				{
					if (type==searchMenu)										//TE: search menu action.
					{
						doSearch(names[list.getSelectedIndex()].toString(), toolTps[list.getSelectedIndex()]);
						//SearchExecute.run(jxplorer.getSearchTree(), jxplorer.getActiveTree().getCurrentDN(), toolTps[list.getSelectedIndex()], null, 2, jxplorer.getSearchBroker());
						//jxplorer.getTreeTabPane().setSelectedComponent(jxplorer.getResultsPanel());
						searchMenu.getPopupMenu().setVisible(false);			//TE: kill the menu (wont do it automatically with the list in it).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -