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

📄 htmltemplatedisplay.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

            if (templateName.length() > prefixSize)
            {
                String templateRelativeFileName = CBParse.replaceAllChar(new StringBuffer(templateName), '/', File.separator);

                File pluginTemplateFile = new File(pluginDirectory + File.separator + templateRelativeFileName);

                try
                {
                    // See if we need to unpack this template file because it doesn't already exist, or is newer than
                    // the cached version.
                    if ((pluginTemplateFile.exists() == false) ||
                            (pluginTemplateFile.lastModified() < resourceLoader.getLastModified(templateName)))
                    {
                        if (templateRelativeFileName.endsWith(File.separator)) // it's a directory - just make it!
                        {
                            pluginTemplateFile.mkdirs();
                        }
                        else
                        {
                            if (!pluginTemplateFile.getParentFile().exists())
                                pluginTemplateFile.getParentFile().mkdirs();

                            byte[] b = resourceLoader.getResource(templateName);	//TE: read the resource into a byte array.

                            try
                            {
                                FileOutputStream output = new FileOutputStream(pluginTemplateFile);
                                output.write(b);
                                output.close();
                            }
                            catch (java.io.IOException e)
                            {
                                CBUtility.error("Problem writing unpacked plugin template file: " + pluginTemplateFile + " to disk \n  ", e);
                            }
                        }
                    }
                }
                catch (ZipException e)
                {
                    CBUtility.error("Problem accessing plugin zip file: " + e);
                }
            }
        }
/*XXX NEEDED???
        if (zipTemplates!=null)
        {	//TE: add any templates to the combo box...
            for(int j=0;j<zipTemplates.length;j++)
            {
                if (zipTemplates[j].endsWith(".html") || zipTemplates[j].endsWith(".htm"))
                {
                    templateNames.add(zipTemplates[j].substring(10));	//TE: cut the prefix 'templates/' off the name of the HTML template for normal oc display handling.
                }
            }
        }
*/
    }

    /**
     * CONSTRUCTOR METHOD: Finds and sets up the start page displayed when JX first loads.  This can be localised.
     * XXX
     * XXX URL MAGIC: This will fail on pathalogical urls, since Sun's file URL Loader can't cope with special
     * XXX            characters in file names (e.g. '#', '@' etc).  We go to insane lengths to handle this in the
     * XXX            template stuff by loading the html text manually via a file reader, and then massaging the
     * XXX            html - but I can't be bothered doing it for this one special case.
     * XXX
     */
    private void setStartPage()
    {

        if (editor == null) editor = getNewEditor();


        String htmldocs = myProperties.getProperty("dir.htmldocs", JXplorer.localDir + "htmldocs" + File.separator);
        try
        {
            final String language = Locale.getDefault().getLanguage();
            File localeSpecificStartFile = new File(htmldocs + startFile + "_" + language + ".html");
            if (localeSpecificStartFile.exists() == false)
            {
                log.warning("unable to find locale specific start file: " + localeSpecificStartFile);
                localeSpecificStartFile = new File(htmldocs + startFile + ".html");
                if (localeSpecificStartFile.exists() == false)
                {
                    log.warning("unable to find locale specific start file: " + localeSpecificStartFile);
                    editor.setText("<html><head><title>JXplorer Start Screen</title></head>" +
                                   "<body><h2><font face=\"arial\">Welcome to JXplorer...</font></h2><p>" +
                                   "<font face=\"arial\">This panel will display the results of your directory browsing and searches.</font></p>" +
                                   "<p><font face=\"arial\">If you need any assistance, use the help option on the menu bar above.</font></p>" +
                                   "</body></html>");
                    validate(); // necessary?
                    return;
                }
            }
            openPage(localeSpecificStartFile.toURL());
            validate();  // necessary?
        }
        catch (IOException e)
        {
            log.warning("unable to open welcome page.  " + e);
        }
    }

    /**
     * <p>CONSTRUCTOR METHOD: Sets up the GUI components; the combo box and the display pane.</p>
     * @param owner the parent GUI to initialise from.
     */
    private void initGUI(Component owner)
    {
        setLayout(new BorderLayout());
        NODATA = "<html><head><title>" + CBIntText.get("No Data") + "</title></head><body><h2><font face=\"arial\">" + CBIntText.get("Select an entry to view data") + "</font></h2></body></html>";
        NOVALUEFOUND = "<i>" + CBIntText.get("No Value Found") + "</i>";
        viewport = new JViewport(); // was CBViewport
        scrollDisplay = new JScrollPane();
        initToolBar();
        add(toolBar, BorderLayout.NORTH);
        add(scrollDisplay);
        htmlEditorKit = new MyHTMLEditorKit(this);
        hyperlinkListener = new MyHyperlinkListener();
        editor = getNewEditor();
        viewport.setView(editor);
        scrollDisplay.setViewport(viewport);
        display = owner;
    }

    private void initMyProperties(Properties props)
    {
        if (props == null)
        {
            CBUtility.error("unable to find html templates", new Exception("Null properties list passed to HTML template display - unable to use templates..."));
            myProperties = new Properties();
        }
        else
            myProperties = props;
    }

    /**
     *	Checks whether a particular candidate string corresponds to the name of a template
     *  in the viewTemplates combo box.
     *  @return whether it exists.
     */

    protected boolean templateExists(String candidate)
    {
/*
    	if (candidate.endsWith(".htm"))									// trim .htm?
    		candidate = candidate.substring(0, candidate.length()-4);   // extension to match
    	else if(candidate.endsWith(".html"))						    // with template names
    		candidate = candidate.substring(0, candidate.length()-5);
*/


        for (int i = 0; i < viewTemplates.getItemCount(); i++)
        {
            String name = (String) viewTemplates.getItemAt(i);
            if (name.equalsIgnoreCase(candidate)) return true;          // CASE SENSITIVE CODE
        }
        return false;
    }

    /**
     *    Sets up the initial tool bar.  The toolbar should have different numbers of
     *    components visible, depending on the users privileges and whether
     *    or not they're actively editing the page...
     */

    public void initToolBar()
    {
        toolBar = new JToolBar();
        String[] errorMessage = {CBIntText.get("no templates found")};
        String[] templates = readTemplateNames(new String[]{});
        if (templates == null || templates.length == 0)
            templates = errorMessage;

        viewTemplates = new CBJComboBox(templates);
        viewTemplates.setEditable(false);
        viewTemplates.setAlignmentY(Component.TOP_ALIGNMENT);

        viewTemplates.setToolTipText(CBIntText.get("Select a template to view attributes with"));

        toolBar.add(viewTemplates);

        /*
         *    Add Action Listeners to the different Tool Bar Components
         */
        viewTemplates.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (settingUpTemplates == true) return;              // ignore 'changes' that are simply adding options to combo box.

                if ((e == null) || (e.getSource() == null)) return;     // sanity check
                if (viewTemplates.getSelectedItem() == null) return; // sanity check

                String templateName = viewTemplates.getSelectedItem().toString();
                if (templateName.equalsIgnoreCase(currentTemplateName)) return;   // nothing *really* happened  // CASE SENSITIVE CODE

                setNewTemplate(templateName);

                repaint();
            }
        });

    }


    public void setNewTemplate(String templateName)
    {
        String current = (String) viewTemplates.getSelectedItem();
        if (templateName.equalsIgnoreCase(current) == false)                       // CASE SENSITIVE CODE
        {
            viewTemplates.setSelectedItem(templateName);
        }
        currentTemplateName = templateName;

        openNewTemplate(templateName);
        displayEntry(currentEntry, currentDataSource);         // same old data, different template
    }


    /**
     *   Adds an array of files to the templateNames list (which eventually turns
     *   into the displayed combo box).
     */

    private void addFileListToTemplateNames(ArrayList templateNames, File[] fileList)
    {
        if (fileList == null)
        {
            return;
        }

        for (int i = 0; i < fileList.length; i++)
        {
            templateNames.add(fileList[i].toString());
        }
    }

    /**
     *  Creates the set of html template names corresponding to a given set of objectClassNames.
     */

    public String[] readTemplateNames(String[] objectClassNames)
    {
        //printTemplates();

        // This is the list of available templates that will be displayed in the combo box.
        ArrayList templateNames = new ArrayList();

        // Always add in the defaults...
        addFileListToTemplateNames(templateNames, (File[]) templates.get(DEFAULT));

        // Now find the templates for each known object class, and store those.
        for (int classNo = 0; classNo < objectClassNames.length; classNo++)
        {
            String className = objectClassNames[classNo].toLowerCase();
            addFileListToTemplateNames(templateNames, (File[]) templates.get(className));
        }

        // dump the stored names into a string array
        String[] templates = (String[]) templateNames.toArray(new String[templateNames.size()]);

        Arrays.sort(templates);

        return templates;
    }


    /**
     *    Try to open a new template.  Try both ".html" and ".htm"
     *    extensions if necessary.  Start in subdirectory (for object
     *    class specific templates) and then try the default template
     *    directory for general templates.
     */

    public boolean openNewTemplate(String templateName)
    {
        // Try to open the template file in the normal templates directory
        File templateFile = new File(baseTemplateDir, templateName);
        if (templateFile.exists() == false)
        {
            //... if that fails, try to find it in the plugin directory...
            templateFile = new File(pluginTemplateDirectory, templateName);
            if (templateFile.exists() == false)
                return CBUtility.error(display, CBIntText.get("Can't find html template! ") + templateName);
        }

        try
        {
            htmlText = new StringBuffer(CBUtility.readTextFile(templateFile));

            // parse file and set paths correctly
            htmlText = parseNewTemplate(htmlText, templateFile.toURI().toURL());
            baseText = htmlText.toString();  // set the base template text.
        }
        catch (IOException e)
        {
            return CBUtility.error(display, CBIntText.get("Can't read html template! ") + templateFile.getAbsolutePath());

⌨️ 快捷键说明

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