Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / MFC

SAFMQ Store and Forward Message Queue

Rate me:
Please Sign up or sign in to vote.
4.74/5 (13 votes)
16 Jan 20064 min read 84.1K   1.8K   33  
An OpenSource cross-compilable/cross-platform message queue server like MSMQ or MQSeries.
/*
 Copyright 2005 Matthew J. Battey

   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.

This software implements a Java application to manage a SAFMQ server.
		
Created on May 18, 2005
*/
package com.safmq.manager;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * @author matt
 */
public class QueuePermissionEditor extends JPanel implements MouseListener, TableModelListener, ListSelectionListener {
	Queue 						queue;
	JTable						table;
	QueuePermissionsTableModel	model;
	JButton						addActor;
	
	Action applyAction;
	Action reloadAction;
	Action deleteActorAction;
	
	public QueuePermissionEditor(Queue queue) {
		JButton			reload;
		JButton			apply;

		this.queue = queue;

		// Create a general keyboard action		
		deleteActorAction = new SimpleAction("Delete Actor", new Integer(KeyEvent.VK_D),true) {
			public void actionPerformed(ActionEvent e) { deleteSelection(); }
		};

		// Create Actions for the buttons
		applyAction = new SimpleAction("Apply",new Integer(KeyEvent.VK_A),false) {
			public void actionPerformed(ActionEvent e) { doApply(); }
		};
		apply = new JButton(applyAction);
		
		reloadAction = new SimpleAction("Reload",new Integer(KeyEvent.VK_R),false) {
			public void actionPerformed(ActionEvent e) { doReload(); }
		};
		reload = new JButton(reloadAction);

		// Begin setting up the layout.
		GridBagLayout		g = new GridBagLayout();
		GridBagConstraints	c = new GridBagConstraints();
		
		this.setLayout(g);
		
		// Setup the table and add the table to the window
		model = new QueuePermissionsTableModel(queue);
		model.addTableModelListener(this);
		table = new JTable(model);
		JScrollPane tablepane = new JScrollPane(table);
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.gridwidth = GridBagConstraints.REMAINDER;
		this.add(tablepane,c);
		table.addMouseListener(this); // TO Enable Popup menu click detection
		table.getSelectionModel().addListSelectionListener(this);
		
		table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0), deleteActorAction);
		
		// Set the constraints for use with the buttons
		c.anchor = GridBagConstraints.EAST;
		c.insets = new Insets(0,5,0,45);
		c.fill = GridBagConstraints.NONE;
		c.weightx = 1;
		c.weighty = 0.0;
		c.gridwidth = 1;
		
		// Add the "Add Actor" button
		addActor = new JButton(new SimpleAction("Add Actor", new Integer(KeyEvent.VK_C), true) {
			public void actionPerformed(ActionEvent e) {
				doAddActor();
			}
		});
		this.add(addActor,c);
		
		// Add the "Reload" button
		c.insets = new Insets(0,0,0,10);
		c.weightx = 0;
		c.gridwidth = GridBagConstraints.RELATIVE;
		this.add(reload,c);

		// Add the "Apply" button.
		c.gridwidth = GridBagConstraints.REMAINDER;
		this.add(apply,c);
	}

	public void doReload() {
		queue.reload();
	}
	public void doApply() {
		queue.saveChanges();
		applyAction.setEnabled(false);
		reloadAction.setEnabled(false);
	}
	public void doAddActor() {
		try {
			QueueAddActor dlg = new QueueAddActor(queue);
			dlg.show();
			if (dlg.isGood()) {
				Object newActors[] = dlg.getNewActors();
				for(int x=0; x<newActors.length; x++) {
					queue.getPermissions().add(newActors[x]);				
				}
				model.fireTableStructureChanged();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deleteSelection() {
		int row = table.getSelectedRow();
		Vector perms = queue.getPermissions();
		if (row >= 0 && row < perms.size()) {
			int result = JOptionPane.showConfirmDialog(Manager.getInstance(),
					"Are you sure you wish to remove \"" + model.getValueAt(row,QueuePermissionsTableModel.NAME_COL)+"\"?",
					"Permission Deletion", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if (result == JOptionPane.YES_OPTION) {		
				applyAction.setEnabled(true);
				reloadAction.setEnabled(true);
				table.getActionMap().remove(deleteActorAction);
				queue.deletePerm((Queue.perms)perms.get(row));
				model.notifyRowDeletion(row);
			}
		}
	}	

	/**
	 * Handles mouse down events in the table, especially the right-click events.
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger()) {
			// TODO: Implement a menu to add/remove actors from the perms list
		}
	}

	/**
	 * Handles changes to the 
	 */
	public void tableChanged(TableModelEvent e) {
		applyAction.setEnabled(true);
		reloadAction.setEnabled(true);
	}
	
	/**
	 * Enables the delete actor action in response to a new selection taking place.
	 * 
	 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
	 */
	public void valueChanged(ListSelectionEvent e) {
		if (table.getSelectedRow() >= 0)
			table.getActionMap().put(deleteActorAction,deleteActorAction);
		else
			table.getActionMap().remove(deleteActorAction);
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
A programmer for 20 years and professionaly employed for 12, I am currently Cheif Engineer for Pharmacy Chare Professionals, Inc., located in Omaha, NE.

My experience is in the area of OO Design, Application, and programming, technical team leadership, RDBMS applications, ISAM applications, Image Processing, Mathematical image generation, Client-Server business applications, eBusiness applications, XML & EDI B2B communications, Java application development, C/C++ application development, CFML/ASP/VB development, on systems like Win2K/NT/98/95, Linux, Irix, Solaris, and MacOS.

Comments and Discussions