Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Views
- {
- /// <summary>
- /// Okno tworzenia nowego szablonu Excel
- /// </summary>
- public partial class NewExcelTemplatePopUpWindow : ChildWindow
- {
- /// <summary>
- /// WebService obsługi excel'a
- /// </summary>
- private ExcelReaderWSClient excelReaderWS;
- /// <summary>
- /// Dialog otwarcia pliku
- /// </summary>
- private OpenFileDialog openFile;
- /// <summary>
- /// Binarny stream pliku Excel
- /// </summary>
- private byte[] stream;
- /// <summary>
- /// Flaga określająca czy plik został
- /// przesłany poprawnie
- /// </summary>
- private bool isFileUploaded;
- /// <summary>
- /// Datasource
- /// </summary>
- private DomainDataSource domainDataSource;
- /// <summary>
- /// Podstawowy konstruktor
- /// </summary>
- public NewExcelTemplatePopUpWindow()
- {
- InitializeComponent();
- excelReaderWS = new ExcelReaderWSClient();
- this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
- this.filePath.IsReadOnly = true;
- this.ChangeUserBoxesState(false);
- // Stwórz databinding sumujący liczbę kolumn:
- // Nazwa property danego obiektu:
- Binding myBinding = new Binding("Count");
- // Obiekt zawierajacy properte:
- myBinding.Source = this.columnNamesList.Items;
- // Przypisz binding
- this.columnCount.SetBinding(Label.ContentProperty, myBinding);
- this.browseButton.Click += new RoutedEventHandler(BrowseButton_Click);
- this.uploadButton.Click += new RoutedEventHandler(UploadButton_Click);
- this.okButton.Click += new RoutedEventHandler(OKButton_Click);
- this.cancelButton.Click += new RoutedEventHandler(CancelButton_Click);
- this.Unloaded += new RoutedEventHandler(NewExcelTemplatePopUpWindow_Unloaded);
- this.excelReaderWS.GetColumnNamesCompleted += new EventHandler<GetColumnNamesCompletedEventArgs>(ExcelReaderWS_GetColumnNamesCompleted);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void NewExcelTemplatePopUpWindow_Unloaded(object sender, RoutedEventArgs e)
- {
- this.excelReaderWS.CloseAsync();
- this.openFile = null;
- }
- /// <summary>
- /// Konstruktor tworzący nowe okno do wczytania pliku excel'a
- /// </summary>
- /// <param name="excelDomainDataSource">Kontektst usługi nadrzędnej</param>
- public NewExcelTemplatePopUpWindow(DomainDataSource excelDomainDataSource)
- : this()
- {
- this.domainDataSource = excelDomainDataSource;
- }
- /// <summary>
- /// Aktualizuje informację o postępie w procesie aplikacji.
- /// </summary>
- /// <param name="information">Informacja</param>
- private void UpdateStatusInformation(string information)
- {
- this.progresBarInformation.Content = information;
- }
- /// <summary>
- /// Zmienia stan formatek danych
- /// wprowadzanych przez użytkownika.
- /// Nieaktywne formatki są resetowane.
- /// </summary>
- /// <param name="areEnabled"><c>true</c> dla aktywacji formatek</param>
- private void ChangeUserBoxesState(bool areEnabled)
- {
- this.templateNameBox.IsReadOnly = !areEnabled;
- if (!areEnabled)
- this.templateNameBox.Text = "";
- this.templateDescBox.IsReadOnly = !areEnabled;
- if (!areEnabled)
- this.templateDescBox.Text = "";
- }
- /// <summary>
- /// Akcja po kliknięciu w przycisk przeglądaj.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BrowseButton_Click(object sender, RoutedEventArgs e)
- {
- // Stwórz dialog:
- this.openFile = new OpenFileDialog();
- openFile.Multiselect = false;
- openFile.Filter = "Microsoft Excel 97-2003 file | *.xls";
- // Zaktualizuj status i otwórz plik:
- if (this.BoolConversion(openFile.ShowDialog()))
- {
- // Przygotuj kontrolki:
- this.ChangeUserBoxesState(false);
- this.columnNamesList.Items.Clear();
- this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.OpeningFile);
- this.fUploadProgressBar.Visibility = System.Windows.Visibility.Visible;
- this.fUploadProgressBar.IsIndeterminate = true;
- this.isFileUploaded = false;
- this.filePath.Text = openFile.File.Name;
- // Przeczytaj binarnie plik:
- this.stream = null;
- FileStream fs = openFile.File.OpenRead();
- stream = this.ReadFully(fs, 0);
- fs.Close();
- // Ukryj progress bar:
- this.UpdateStatusInformation("");
- this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
- }
- else
- {
- // Ukryj progress bar i pokaż informacje o niepowodzeniu:
- this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
- this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.FileOpenFailed);
- }
- }
- /// <summary>
- /// Akcja po kliknięciu w przycisk upload
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void UploadButton_Click(object sender, RoutedEventArgs e)
- {
- // Prześlij stream do Web Service'u do analizy:
- if (this.stream != null)
- {
- this.columnNamesList.Items.Clear();
- this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.GettingColumnNames);
- this.fUploadProgressBar.Visibility = System.Windows.Visibility.Visible;
- this.excelReaderWS.GetColumnNamesAsync(this.stream, openFile.File.Name);
- }
- }
- /// <summary>
- /// Akcja wykonywana po otrzymania odpowiedzi Web Service
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ExcelReaderWS_GetColumnNamesCompleted(object sender, GetColumnNamesCompletedEventArgs e)
- {
- // Ukryj pasek progresu:
- this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
- if (e.Error == null)
- {
- // Aktywuj kontrolki użytkownika:
- this.isFileUploaded = true;
- this.UpdateStatusInformation("");
- this.ChangeUserBoxesState(true);
- this.templateNameBox.Text = this.filePath.Text.Replace(".xls", "");
- ObservableCollection<string> result = e.Result;
- // Dodaj nazwy kolumn:
- foreach (string column in result)
- {
- this.columnNamesList.Items.Add(column);
- }
- }
- else
- {
- this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.GettingColumnsFailed);
- }
- }
- /// <summary>
- /// Akcja po kliknięciu w przycisk OK
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OKButton_Click(object sender, RoutedEventArgs e)
- {
- // Sprawdź czy plik załadowany jest poprawnie
- if (this.isFileUploaded)
- {
- // Waliduj:
- if (this.ValidateTemplateInformation())
- {
- List<string> result = new List<string>();
- foreach (string s in this.columnNamesList.Items)
- {
- result.Add(s);
- }
- string xmlColumnList = this.SerializeToXMLString(typeof(List<string>), result);
- // Zapisz nowy szablon i zakończ:
- ExcelTemplate template = new ExcelTemplate();
- template.CreationDate = DateTime.Now;
- template.TemplateName = this.templateNameBox.Text;
- template.Description = this.templateDescBox.Text;
- template.XmlColumnNames = xmlColumnList;
- this.domainDataSource.DataView.Add(template);
- this.domainDataSource.SubmitChanges();
- this.DialogResult = true;
- }
- }
- }
- /// <summary>
- /// Waliduje informacje o
- /// szablonie wprowadzane przez użytkownika
- /// </summary>
- /// <returns><c>true</c> jeśli walidacja przebiegła poprawnie</returns>
- private bool ValidateTemplateInformation()
- {
- }
- /// <summary>
- /// Wyszukuje szablon o wskazanej nazwie
- /// w zbiorze obecnych szablonów
- /// </summary>
- /// <param name="name">Nazwa szablonu</param>
- /// <returns><c>true</c> jeśli nazwa szablonu jest unikatowa</returns>
- public bool IsTemplateNameUnique(string name)
- {
- foreach (ExcelTemplate template in this.domainDataSource.DataView)
- {
- if (name.ToLower().Equals(template.TemplateName.ToLower()))
- {
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// Zamyka i anuluje akcje tworzenia
- /// szablonu
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CancelButton_Click(object sender, RoutedEventArgs e)
- {
- this.DialogResult = false;
- }
- /// <summary>
- /// Akcja po zamknięciu okna
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ChildWindow_Closed(object sender, EventArgs e)
- {
- this.DialogResult = false;
- }
- /// <summary>
- /// Akcja po kliknięciu przycisku anuluj
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cancelButton_Click_1(object sender, RoutedEventArgs e)
- {
- this.DialogResult = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement