hkbmwcn

          2007年7月30日 #

          關于JInternalFrame去掉Title bar的問題

          自己實在是個懶人,blog難得更新一次,更新也是一些雞毛蒜皮的小東西。不過還是希望能對其他朋友或自己將來遇到類似問題能有個解答。最新在做一個swing項目,客戶要求能把JInternalFrame的Title bar去掉,同時還能加回來。由于網上搜一下沒有找到解決辦法,只能自己研究一下并改了下JInternalFrame,先記錄如下:


          import java.awt.BorderLayout;
          import java.awt.Dimension;
          import java.awt.Font;
          import java.awt.Rectangle;
          import java.awt.event.ComponentEvent;
          import java.awt.peer.ComponentPeer;
          import java.beans.PropertyVetoException;

          import javax.swing.ActionMap;
          import javax.swing.BorderFactory;
          import javax.swing.JComponent;
          import javax.swing.JDesktopPane;
          import javax.swing.JInternalFrame;
          import javax.swing.UIManager;
          import javax.swing.plaf.InternalFrameUI;
          import javax.swing.plaf.basic.BasicInternalFrameUI;

          public class MCOCInternalFrame extends JInternalFrame {
              
              //private String lookAndFeel = null;
              BasicInternalFrameUI orgUi = null;
              BasicInternalFrameUI newUi = null;
              JComponent northPanel = null;
              private boolean isHidden = false;

              
              public MCOCInternalFrame() {
                  super();

                  northPanel = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).getNorthPane();
                  orgUi = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI());
                  newUi = new BasicInternalFrameUI(this);        
              }
              
              
              public void showNorthPanel() {

                  
                  this.setUI(orgUi);
                  this.putClientProperty("JInternalFrame.isPalette", Boolean.FALSE);
                  isHidden = false;

              }
              
              public void hideNorthPanel() {
                  this.setUI(newUi);
                  ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
                  this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
                  isHidden = true;
                  
              }
              
               public void updateUI() {
                  
                      super.updateUI();
                      if (isHidden) {
                          hideNorthPanel();
                      }
               }


          }

          創建該InternalFrame對象后,通過showNorthPanel(), hideNorthPanel()來顯示或隱藏title bar,另外updateUI()重寫是因為界面被動態改變lookandfeel時,保證title bar上多的一小個bar出現。


          posted @ 2008-01-18 22:02 亙古頑石 閱讀(637) | 評論 (0)編輯 收藏

          javax mail 發送郵件及附件

          MailSender.java

          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.Properties;

          import javax.activation.DataHandler;
          import javax.activation.DataSource;
          import javax.activation.FileDataSource;
          import javax.mail.MessagingException;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.AddressException;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeBodyPart;
          import javax.mail.internet.MimeMessage;
          import javax.mail.internet.MimeMultipart;

          import org.apache.log4j.Logger;

          public class MailSender {
           public static Logger logger = Logger.getLogger(MailSender.class);
           public static boolean send(Mail mail) throws Exception {
            try {
             Properties props = new Properties();
             props.put("mail.smtp.host", "localhost");
             Session session = Session.getDefaultInstance(props, null);
             MimeMessage mimemessage = new MimeMessage(session);
             mimemessage.setFrom(new InternetAddress(mail.getFrom()));
             mimemessage.setSentDate(mail.getDate());
             // set SUBJECT
             mimemessage.setSubject(mail.getSubject());

             // set TO address
             String mailto = mail.getTo();
             String ccmailid = mail.getCcusers();
             String strResult = "";
             try {
              mimemessage.setRecipients(javax.mail.Message.RecipientType.TO,
                mailto);
             } catch (Exception exception1) {
              throw exception1;
             }

             // set message BODY
             MimeBodyPart mimebodypart = new MimeBodyPart();
             mimebodypart.setText(mail.getContent());

             // attach message BODY
             MimeMultipart mimemultipart = new MimeMultipart();
             mimemultipart.addBodyPart(mimebodypart);

             // attach FILE
             ArrayList attachedFileList = mail.getAttachedFileList();
             if (attachedFileList != null) {
              DataSource ds = null;;
              for (Iterator e = attachedFileList.iterator(); e.hasNext();) {
               ds = (DataSource) e.next();
               mimebodypart = new MimeBodyPart();
               try {
                mimebodypart.setDataHandler(new DataHandler(
                  ds));
               } catch (Exception exception3) {
                throw exception3;
               }
               mimebodypart.setFileName(ds.getName()); // set FILENAME
               mimemultipart.addBodyPart(mimebodypart);
              }
             }// end if
             mimemessage.setContent(mimemultipart);
             // set CC MAIL and SEND the mail
             if (!mailto.equals("")) {
              // set CC MAIL
              if (ccmailid != null && (!ccmailid.equals("")))
               mimemessage.setRecipients(
                 javax.mail.Message.RecipientType.CC, ccmailid);
              try {
               // send MAIL
               Transport.send(mimemessage);
               logger.info(mailto + " Sent Successfully..........");
              } catch (Exception exception4) {
               throw exception4;
              }
             } else {
              logger.info(mailto + " Mail operation Failed..........");
             }
            } catch (Exception e) {
             throw e;
            }
            return true;
           }

          }

          Mail.java
          import java.util.ArrayList;
          import java.util.Date;
          import java.util.StringTokenizer;

          public class Mail {
           
           private String from = null;
           private String to = null;
           private String subject = null;
           private String content = null;
           private String ccusers = null;
           private ArrayList attachedFileList = null;
           private Date date = null;

           public Mail() {
            // TODO Auto-generated constructor stub
           }

           public ArrayList getAttachedFileList() {
            return attachedFileList;
           }

           public void setAttachedFileList(ArrayList attachedFileList) {
            this.attachedFileList = attachedFileList;
           }

           


           public String getContent() {
            return content;
           }

           public void setContent(String content) {
            this.content = content;
           }

           public String getFrom() {
            return from;
           }

           public void setFrom(String from) {
            this.from = from;
           }

           public String getSubject() {
            return subject;
           }

           public void setSubject(String subject) {
            this.subject = subject;
           }

           public String getTo() {
            return to;
           }

           public void setTo(String to) {
            this.to = to;
           }

           public Date getDate() {
            return date;
           }

           public void setDate(Date date) {
            this.date = date;
           }

           public String getCcusers() {
            return ccusers;
           }

           public void setCcusers(String ccusers) {
            this.ccusers = ccusers;
           }

          }




          posted @ 2008-01-18 21:50 亙古頑石 閱讀(2071) | 評論 (1)編輯 收藏

          密碼的加解密

          很長時間沒有更新自己的blog了,最近忙一個基于alfreco的項目開發,趕快把用到的一些東西記錄一下,誰讓自己記性不好呢。這里是password的加解密:
          PasswordUtil.java

          import java.io.IOException;
          import java.security.MessageDigest;
          import java.security.NoSuchAlgorithmException;
          import java.security.Security;

          import javax.crypto.Cipher;
          import javax.crypto.KeyGenerator;
          import javax.crypto.SecretKey;

          import sun.misc.BASE64Decoder;
          import sun.misc.BASE64Encoder;

          public class PasswordUtil {
           public final static int ITERATION_NUMBER = 1000;
           public static final String algorithm = "Blowfish";// we can use DES,DESede,Blowfish
           
           /**
            *
            * @param password
            * @return
            * @throws Exception
            */
           public static PasswordBean encry(String password) throws Exception {
            Security.addProvider(new com.sun.crypto.provider.SunJCE());

            try {
             KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
             SecretKey deskey = keygen.generateKey();
             Cipher c1 = Cipher.getInstance(algorithm);
             c1.init(Cipher.ENCRYPT_MODE, deskey);
             byte[] cipherByte = c1.doFinal(password.getBytes());
             String saltKey = PasswordUtil.byteToBase64(deskey.getEncoded());
             String pass = PasswordUtil.byteToBase64(cipherByte);
             return new PasswordBean(saltKey, pass);
             //System.out.println("After encry:" + key + "====" + password);
            } catch (Exception e) {
             throw e;
            }
           }
           
           /**
            *
            * @param bean
            * @return
            * @throws Exception
            */
           public static String decry(PasswordBean bean) throws Exception {
            return decry(bean.getSaltKey(), bean.getPassword());
           }
           
           /**
            *
            * @param saltKey
            * @param pass
            * @return
            * @throws Exception
            */
           public static String decry(String saltKey, String pass) throws Exception {
            //Security.addProvider(new com.sun.crypto.provider.SunJCE());
            try {
             Security.addProvider(new com.sun.crypto.provider.SunJCE());
             byte[] keyser = PasswordUtil.base64ToByte(saltKey);
             javax.crypto.spec.SecretKeySpec destmp = new javax.crypto.spec.SecretKeySpec(keyser, algorithm);
             SecretKey mydeskey = destmp;

             Cipher c1 = Cipher.getInstance(algorithm);
             c1.init(Cipher.DECRYPT_MODE, mydeskey);
             byte[] clearByte = c1.doFinal(PasswordUtil.base64ToByte(pass));
             return new String(clearByte);
            } catch (Exception e) {
             //e.printStackTrace();
             System.out.println("saltKey:" + saltKey + "   pass:" + pass) ;
             throw e;
            }
           }
           
           
           
           /**
            * From a password, a number of iterations and a salt, returns the
            * corresponding digest
            *
            * @param iterationNb
            *            int The number of iterations of the algorithm
            * @param password
            *            String The password to encrypt
            * @param salt
            *            byte[] The salt
            * @return byte[] The digested password
            * @throws NoSuchAlgorithmException
            *             If the algorithm doesn't exist
            */
           public static byte[] getHash(int iterationNb, String password, byte[] salt)
             throws NoSuchAlgorithmException {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            digest.update(salt);
            byte[] input = digest.digest(password.getBytes());
            for (int i = 0; i < iterationNb; i++) {
             digest.reset();
             input = digest.digest(input);
            }
            return input;
           }

           /**
            * From a base 64 representation, returns the corresponding byte[]
            *
            * @param data
            *            String The base64 representation
            * @return byte[]
            * @throws IOException
            */
           public static byte[] base64ToByte(String data) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            return decoder.decodeBuffer(data);
           }

           /**
            * From a byte[] returns a base 64 representation
            *
            * @param data
            *            byte[]
            * @return String
            * @throws IOException
            */
           public static String byteToBase64(byte[] data) {
            BASE64Encoder endecoder = new BASE64Encoder();
            return endecoder.encode(data);
           }
          }

          PasswordBean.java

          public class PasswordBean {
           
           private String saltKey = null;
           private String password = null;

           public PasswordBean(String key, String pass) {
            this.saltKey = key;
            this.password = pass;
           }
           
           public String getPassword() {
            return password;
           }
           public void setPassword(String password) {
            this.password = password;
           }
           public String getSaltKey() {
            return saltKey;
           }
           public void setSaltKey(String saltKey) {
            this.saltKey = saltKey;
           }
           
          }


          使用的時候可以是:
          String password = PasswordUtil.decry(salt, encodePassword);
          PasswordBean passwordBean = PasswordUtil.encry(password);

          posted @ 2007-07-30 10:42 亙古頑石 閱讀(355) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 东山县| 怀仁县| 抚远县| 乌苏市| 黎川县| 工布江达县| 福州市| 衢州市| 河曲县| 乳源| 永春县| 马关县| 宁德市| 平舆县| 鄂温| 上高县| 曲麻莱县| 杨浦区| 镇原县| 黄平县| 类乌齐县| 石首市| 五大连池市| 九寨沟县| 汉阴县| 阿克苏市| 西安市| 吉林省| 清涧县| 金沙县| 清新县| 运城市| 乌兰浩特市| 陇西县| 嫩江县| 榆林市| 潜山县| 龙川县| 赣榆县| 玉龙| 东宁县|