headeraction.java

来自「jetspeed源代码」· Java 代码 · 共 872 行 · 第 1/3 页

JAVA
872
字号
/*
 * Copyright 2000-2001,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jetspeed.modules.actions.portlets.designer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;

import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.services.resources.TurbineResources;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.upload.FileItem;
import org.apache.velocity.context.Context;

/**
 * Header Action
 * 
 * @author <a href="mailto:jlim@gluecode.com">Jonas Lim</a>
 * @version $Id: HeaderAction.java,v 1.2 2004/03/22 22:26:58 taylor Exp $
 */
public class HeaderAction extends VelocityPortletAction
{
    private static final JetspeedLogger log = JetspeedLogFactoryService.getLogger(HeaderAction.class.getName());
    private static final String IMAGES_DIRECTORY = "images/designer/";
    private static final String DEFAULT_CSS = "css/default.css";

    private String fileTypes[] =
    { "image/jpg", 
      "image/gif", 
      "image/jpeg",
       "application/x-shockwave-flash",
       "image/png",
       "image/x-png"
    };
    
    
    protected void buildNormalContext(VelocityPortlet portlet, Context context,
            RunData rundata) throws Exception
    {

    }

    public void doUpload(RunData rundata, Context context)
    {
        Hashtable ht = new Hashtable();

        boolean setLogo = rundata.getParameters().getBoolean("setLogo");
        boolean setBgImage = rundata.getParameters().getBoolean("setBgImage");
        boolean setBgColor = rundata.getParameters().getBoolean("setBgColor");
        boolean setFontFace = rundata.getParameters().getBoolean("setFontFace");
        boolean setFontSize = rundata.getParameters().getBoolean("setFontSize");
        boolean setFontColor = rundata.getParameters().getBoolean(
                "setFontColor");
        boolean setTitle = rundata.getParameters().getBoolean("setTitle");

        FileItem fileLogo = null;
        FileItem fileBgImage = null;
        String bgColor = "";
        String fontFace = "";
        String fontSize = "";
        String fontColor = "";
        String bgImage = "";
        String title = "";

        if (setLogo)
        {
            fileLogo = rundata.getParameters().getFileItem("imgLogo");
        }

        if (setBgImage)
        {
            String sampleBg = rundata.getParameters()
                    .getString("sampleBgImage").trim();
            if (sampleBg != null && sampleBg.length() != 0)
            {
                bgImage = IMAGES_DIRECTORY + sampleBg;
            } else
            {
                fileBgImage = rundata.getParameters().getFileItem("bgImage");
            }
        }

        if (setBgColor)
        {
            bgColor = rundata.getParameters().getString("bgColor").trim();
        }
        if (setFontFace)
        {
            fontFace = rundata.getParameters().getString("fontFace").trim();
        }
        if (setFontSize)
        {
            fontSize = rundata.getParameters().getString("fontSize").trim();
        }
        if (setFontColor)
        {
            fontColor = rundata.getParameters().getString("fontColor").trim();
        }
        if (setTitle)
        {
            title = rundata.getParameters().getString("portalTitle").trim();
        }

        log.info("fontColor : " + fontColor);

        String message = "";
        try
        {
            String logo = "";

            String slash = System.getProperty("file.separator");
            String imagePath = FormatPath.normalizeDirectoryPath(rundata
                    .getServletContext().getRealPath("/" + IMAGES_DIRECTORY));

            if (fileLogo != null)
            {
                File file = new File(fileLogo.getFileName());
                logo = file.getName();

                int index = logo.lastIndexOf("\\");
                int index2 = logo.lastIndexOf("//");

                if (index > 0)
                {
                    logo = logo.substring(index + 1);
                }

                if (index2 > 0)
                {
                    logo = logo.substring(index2 + 1);
                }

                File f = new File(imagePath + logo);
                if (f.exists()) 
                {
                    f.delete();//nik
                }
                FileUploader fu = new FileUploader();
                boolean hasUploaded = fu.upload(fileLogo, imagePath, fileTypes);
                //String filename = fu.getFilename(fileLogo, imagePath,
                // fileTypes);

                if (hasUploaded == true)
                {
                    logo = IMAGES_DIRECTORY + logo;
                } else
                {
                    context
                            .put(
                                    "logoStatus",
                                    "Error occurred while uploading "
                                            + logo
                                            + ". Only gif, jpg, and jpeg image files can be uploaded. ");
                    logo = "";
                }

                //context.put("logoFile", filename);

            }

            if (fileBgImage != null)
            {
                File file1 = new File(fileBgImage.getFileName());
                bgImage = file1.getName();

                int index = bgImage.lastIndexOf("\\");
                int index2 = bgImage.lastIndexOf("//");

                if (index > 0)
                {
                    bgImage = bgImage.substring(index + 1);
                }

                if (index2 > 0)
                {
                    bgImage = bgImage.substring(index2 + 1);
                }

                File f = new File(imagePath + bgImage);
                if (f.exists()) f.delete();//nik
                FileUploader fu = new FileUploader();
                boolean hasUploaded = fu.upload(fileBgImage, imagePath,
                        fileTypes);
                log.info("hasUploaded : " + hasUploaded);

                if (hasUploaded == true)
                    bgImage = IMAGES_DIRECTORY + bgImage;
                else
                {
                    context
                            .put(
                                    "bgStatus",
                                    "Error while uploading "
                                            + bgImage
                                            + ". Only gif, jpg, and jpeg image files can be uploaded. ");
                    bgImage = "";
                }

            }

            boolean hasColor = (bgColor == null || bgColor.length() < 1);
            boolean hasFontFace = (fontFace == null || fontFace.length() < 1);
            boolean hasFontSize = (fontSize == null || fontSize.length() < 1);
            boolean hasFontColor = (fontColor == null || fontColor.length() < 1);

            if (hasColor)
            {
                bgColor = "white";
            }
            if (hasFontFace)
            {
                fontFace = "verdana";
            }
            if (hasFontSize)
            {
                fontSize = "12";
            }
            if (hasFontColor)
            {
                fontColor = "black";
            }

            ht.put("logo", logo);
            ht.put("BgImage", bgImage);
            ht.put("BgColor", bgColor);
            ht.put("FontFace", fontFace);
            ht.put("FontSize", fontSize);
            ht.put("FontColor", fontColor);
            ht.put("PortalTitle", title);
            ht.put("SetLogo", String.valueOf(setLogo));
            ht.put("SetBgImage", String.valueOf(setBgImage));
            ht.put("SetBgColor", String.valueOf(setBgColor));
            ht.put("SetFontFace", String.valueOf(setFontFace));
            ht.put("SetFontSize", String.valueOf(setFontSize));
            ht.put("SetFontColor", String.valueOf(setFontColor));
            ht.put("SetTitle", String.valueOf(setTitle));

            editHeader(rundata, context, ht);

        } catch (Exception ee)
        {
            message = ee.getMessage();
        }
    }

    public void editHeader(RunData rundata, Context context, Hashtable ht)
            throws Exception
    {
        try
        {
            String logo = (String) ht.get("logo");
            String bgImage = (String) ht.get("BgImage");
            String bgColor = (String) ht.get("BgColor");
            String fontSize = (String) ht.get("FontSize");
            String fontColor = (String) ht.get("FontColor");
            String title = (String) ht.get("PortalTitle");

            boolean setLogo = Boolean.valueOf((String) ht.get("SetLogo")).booleanValue();
            boolean setBgImage = Boolean.valueOf((String) ht.get("SetBgImage"))
                    .booleanValue();
            boolean setBgColor = Boolean.valueOf((String) ht.get("SetBgColor"))
                    .booleanValue();
            boolean setFontSize = Boolean.valueOf((String) ht.get("SetFontSize"))
                    .booleanValue();
            boolean setFontColor = Boolean.valueOf((String) ht.get("SetFontColor"))
                    .booleanValue();
            boolean setPortalTitle = Boolean.valueOf((String) ht.get("SetTitle"))
                    .booleanValue();

            String DEFAULT_ROOT = File.separator + "WEB-INF" + File.separator
                    + "conf" + File.separator;
            String root = rundata.getServletConfig().getServletContext()

⌨️ 快捷键说明

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