Hi Marc,

You can extend AllPermissionFileService and override the following method:

FileContents storeFile(final String filePath, final InputStream in) throws
ServiceException {}

Or

You can implement your own file service.

Source code for AllPermissionsFileService is part of ULC release and it can
be found in:

      <ULCInstallDir>\base\src\ulc-base-trusted-src.jar

At the end of this mail is a snippet which demonstrates how you can add an
extension to the file name.

I hope this helps.

Thanks and regards,

Janak

>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] Behalf Of Marc Fritsche
>Sent: Wednesday, June 14, 2006 6:41 PM
>To: [email protected]
>Subject: [ULC-developer] Altering filepath when saving file
>
>
>Hi!
>
>I am trying to save a text within a file on the client. For that purpose
>I show a FileChooser and let the user select the desired filename using
>the method
>
>ClientContext.storeFile(IFileStoreHandler, FileChooserConfig, ULCComponent)
>
>I implement the callback prepareFile(OutputStream) of the
>IFileStoreHandler interface to write the desired file content into the
>OutputStream. As soon the user has selected the filename and approved
>the FileChooser the callback onSuccess(String) is beeing called. At this
>point I have no chance to alter the filename. This is especially desired
>if the user does not specify a file extension. I usually check whether
>the entered filename ends with the required extension and if not, I
>append it. Is there any other possibility to solve this problem? I guess
>I could rename the file afterwards but I am looking for a more
>elegant way..
>
>Thanks in advance
>Marc
>_______________________________________________
>ULC-developer mailing list
>[email protected]
>http://lists.canoo.com/mailman/listinfo/ulc-developer
-----------------------------------------------------------


import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.BorderFactory;
import com.ulcjava.base.application.ClientContext;
import com.ulcjava.base.application.ULCAlert;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTextArea;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.serializable.IActionListener;
import com.ulcjava.base.application.util.DefaultFileStoreHandler;
import com.ulcjava.base.client.ClientEnvironmentAdapter;
import com.ulcjava.base.client.FileContents;
import com.ulcjava.base.client.ServiceException;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.shared.FileChooserConfig;
import com.ulcjava.base.trusted.AllPermissionsFileService;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;

public class ChangeFileNameInFileServiceSnippet extends AbstractApplication
{
    public void start() {
        ULCBoxPane content = createContent();

        ULCFrame frame = new ULCFrame("ChangeFileNameInFileServiceSnippet");
        frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
        frame.add(content);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }

    private ULCBoxPane createContent() {
        ULCBoxPane demo = new ULCBoxPane(true);
        ULCBoxPane fileBox = new ULCBoxPane(true);
        fileBox.setBorder(BorderFactory.createTitledBorder("Save text to a
local file"));
        final ULCTextArea textArea = new ULCTextArea("", 10, 40);
        textArea.setLineWrap(true);
        fileBox.add(ULCBoxPane.BOX_EXPAND_EXPAND, new
ULCScrollPane(textArea));

        ULCBoxPane buttonBox = new ULCBoxPane(false);


        final ULCButton saveButton = new ULCButton("Save local");
        saveButton.addActionListener(new IActionListener() {
            public void actionPerformed(ActionEvent event) {
                FileChooserConfig config = new FileChooserConfig();
                config.setDialogTitle("Choose where to save typed text");
                config.setApproveButtonText("Save");
                config.addFileFilterConfig(new
FileChooserConfig.FileFilterConfig(new String[] { ".txt", ".inf" },
                        "description (*.txt, *.inf)"));
                try {
                    ClientContext.storeFile(new
DefaultFileStoreHandler(textArea.getText().getBytes()) {
                        public void onSuccess(String filePath) {
                            new ULCAlert("Message", "text saved to " +
filePath, "Close").show();
                        }

                        public void onFailure(int reason, String
description) {
                            new ULCAlert("Message", "operation not performed
(" + description + ")", "Close").show();
                        }

                    }, config, saveButton);
                } catch (Exception ignored) {
                }
            }
        });

        buttonBox.add(saveButton);
        fileBox.add(ULCBoxPane.BOX_CENTER_CENTER, buttonBox);
        demo.add(ULCBoxPane.BOX_EXPAND_EXPAND, fileBox);
        return demo;
    }


    public static void main(String[] args) {
        ClientEnvironmentAdapter.setFileService(new MyFileService());
        DevelopmentRunner.setApplicationClass(ChangeFileNameInFileServiceSni
ppet.class);
        DevelopmentRunner.main(args);
    }


    public static class MyFileService extends AllPermissionsFileService {
        @Override
        public FileContents storeFile(final String filePath, final
InputStream in) throws ServiceException {

            try {
                FileContents fileContents = (FileContents)AccessController
                        .doPrivileged(new PrivilegedExceptionAction() {
                            public Object run() throws Exception {
                                // Check for extension
                                String fileName = (filePath.indexOf('.')
!= -1 ? filePath : filePath + ".txt");
                                BufferedOutputStream out = new
BufferedOutputStream(new FileOutputStream(fileName));
                                int b;
                                while ((b = in.read()) != -1) {
                                    out.write(b);
                                }
                                out.close();

                                File file = new File(fileName);
                                return new
FileContents(file.getAbsolutePath());
                            }
                        });
                return fileContents;
            } catch (Exception e) {
                throw new ServiceException("could not store file", e);
            }
        }
    }
}


_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to